InvalidateSize without screen flash? - apache-flex

Here's the scenario: I wait for a creationComplete event to occur for an mx:Text object at which point I can access the setTextFormat method of its protected member textField. (textField is not valid until creationComplete.) At that point the text formatting done through textField.setTextFormat increases the height of the text. But the mx:Text itself does not pick up this height change until I call invalidateSize. However, in my case invalidateSize causes the entire text to be redrawn, causing it to flash on the screen. However, if I just manually change the browser window size, the Text height change is made without the text flashing like that. So how could I through a function call accomplish what is taking place when I manually change the browser window size. (I just want mx:Text to pick up the height change accomplished through textfield.setTextFormat.)

did you tried to put your textfield inside a sprite or movieclip and scale it with your callback?

to invertedspear
I was in fact able to avoid the screen flash by calling
txt1.invalidateSize();
txt1.validateNow();
immediately after the calls to txt1.ui_txtfld().setTextFormat(....)
(ui_txtfld() is how I'm making the protected property textField visible.)
However, elsewhere in my code, it was also necessary to not reference txt1.height directly, but instead,
(txt1.ui_txtfld().textHeight+4)*txt1.scaleY,
as the txt1.textField.textHeight property is valid immediately after changing the height of a textField, whereas txt1.height is not.

Related

How to determine whether a QML component is currently visible on screen

I have a set of controls which have bindings to frequently changing data values. The data comes from a limited hardware-bus. Therefore, it would be better to disable the binding while the control is not visible on screen. The item's visible-property doesn't help in this case. So, how to determine if an Item-based QML widget is currently visible on screen (and not hidden by an overlay or currently outside the visible area)?
Source: https://forum.qt.io/topic/54116/how-to-check-if-a-item-is-currently-visible-on-screen
I have almost the same problem. Hoping someone here has a solution.
Here's what I would try to get working:
First, I presume there is a ScrollView or Flickable in play here? If so, then hook to signals like Flickable::movementEnded().
Second, when that signal fires, use Item::mapToItem() to check if each of your Item's visible rectangle (based on x, y, width, height) intersects your window's contentItem rectangle. Set the result as a boolean on each of your items and make sure the data retrieval is disabled when it is false (using && or a tertiary JS expression).
Or if more convenient, remove the binding when false and reapply it with Qt.binding() when true.

Getting QScrollArea to resize its child widget immediately

I have a QScrollArea (we'll call it myContainer) that contains and scrolls a child view (we'll call it myChildWidget). All works almost fine - when I change the height of myChildWidget dynamically in response to something, if the height exceeds that of myContainer (the QScrollArea), a vertical scroll bar pops into view, effectively narrowing myChildWidget since I've set it to resize its child using:
myContainer->setWidgetResizable(true);
The problem is that myChildWidget is not actually resized until later, rather than right when I set its new height or try resizing myContainer, so I can't do certain things depending on its new width without subclassing and putting in a whole bunch of extra code. Surely there's something I can call to get the QScrollArea to auto-resize its child right away, right? I can use:
QCoreApplication::processEvents();
but the problem with that is that it causes the widgets to flash and redraw when I'm not done setting things up. I've tried update(), updateGeometry(), and adjustSize(), both on the container and its child, and none work. Anything I'm missing? Thanks for any help.

Centering a QDialog relative to its parent while using RestoreGeometry()

I have a program that has several QDialogs. Most are a fixed size, but one is a resizable editor. The editor dialog gets its geometry restored from a QByteArray.
The problem I'm having is the Editor is not being centered correctly. (only the window's x/y dimensions) The Editor usually appears in the upper left corner when it is instantiated.
The Editor class gets instantiated like this in MainWindow:
Editor e(this);
e.exec();
For some reason, restoring the window geometry breaks the Editor's automatic centering relative to its parent. Before I implemented the SaveGeometry and RestoreGeomentry code today, the editor worked fine.
Here's the SaveGeometry code in Editor.cpp: (it gets executed right before the current Editor object is destroyed)
// Remember current window size
QByteArray geo=saveGeometry();
SettingsManager s;
s.SaveEditorSize(geo);
Here's the restore code in Editor.cpp: (this is part of a function that sets up the UI, assigns a layout, etc.)
if(!Buffer::editor_geometry.isEmpty()){
restoreGeometry(Buffer::editor_geometry);
}
Does anyone know what the problem is?
Edit: Actually, it does remember where it was last time, it just won't center.
Actually, I think I've solved this.
Instead of storing the entire window geometry, I tried storing just the size as a QSize. The size is then reapplied through QDialog::Resize() when the Editor is initialized. The window remembers its previous size and remains centered to parent like it is supposed to.

How do I auto-adjust the size of a QDialog depending on the text length of one of its children?

I have a QDialog I'm working with. It is made somewhat like a QMessageBox. I noticed that the size of the QMessageBox (and the size of its label) depends on the size of the message displayed.
How would I make the size of my QDialog adjust automatically like a QMessageBox? Presently my QDialog contains a button box and a label, and the QDialog is layout Vertical.
(I know I could just use the message box directly but eventually I will have more complex dialogs.)
Automatic solution:
Use layouts and set size policies to QSizePolicy::Expanding. In QtDesigner, once all your children are placed on your QDialog, then click on the Adjust Size button next layout ones. Your QDialog will be automatically resized at runtime.
Manual solution:
The QWidget class has a method adjustSize that resize the QWidget to fit its content. Just call it when all children are set.
Set your dialog to be expanding, and very small. Then, be sure to set your message before showing the dialog. When shown, it will try to find its proper size, based on the size of the objects it contains. (This happens recursively, so if the dialog isn't the direct parent of the label in which you show your message, make sure everything between the label and the dialog is set to use layouts.)
A TIP : if you try to use "adjustSize()" function when dialog is hidden, it may not be works fine. It would be better to use it after the "show()" function.

How do I enable autoresizing for a Flex UIComponent?

I have a class which extends UIComponent and draws directly onto a Sprite contained within. Currently I'm (probably incorrectly) listening to the Event.RESIZE event and drawing the contents when the width and height are non-zero. The problem is that even though I've passed percentage widths to the instance tag, it doesn't appear to be resized along with other Flex components on the page, certainly the resize event isn't being fired at all.
I've hacked it for the moment by binding the width and height to a container which does resize, but how should I really be handling this?
Update :
It turns out I was setting the width and height somewhere in the redraw method (I have no recollection of why I did this!). I shall go hang my head in shame now...
I think you need to provide more information. I'm doing exactly the same, and it works smoothly for me. Perhaps the answer lies somewhere else: e.g. exactly what type of container do you use? Isn't it possible that the space gained/lost on resizing gets allocated to some other component within the cointainer? Try substituting your own component with an mx:Box with some colored background, and see if that resizes with the container.

Resources