Get yet-not-shown widget's size -> BUG?



  • I have to get a widget's position and size before it actually gets inserted to the layout and shown. (It's for a cross-page fading effect - basically: fade source out and target in, which I managed with a temporary widget which gets the snapshots from both source-page and target-page, makes the transition then replaces itself with the target).

    The strange thing is:

    I tried to do it this way: source is in the layout, so get it's geometry, then remove it from the layout, insert the target to it's place, get it's geometry then remove it - and insert the cross-fader widget to the place. It worked if the target-page has a deterministic size, but for some reason it didn't when it's (contents') size is depending on the layout (e.g. a QPushButton/QLabel with only a text doesn't work but if I set a proper fixedSize or a big-enough minimumSize to it then it works).
    What does "it doesn't work" mean (when the page-size is not deterministic): with the QPushButton example: actually it calculated some size, but the size is smaller then the real size. The cross-fader widget painted these pages sqeezed.

    The solution:

    • to the position do the same as I wrote above: get the sizes from the widgets when they are in the layout (the target is inserted, the layout is updated, get it's position then the target widget is removed). But for the sizes: get them after the widgets are removed from the layout.
      This way everything works correctly.

    Is it a bug, or did I miss something?

    I use Qt 4.7.0 (32 bit), Qt Creator 2.0.1.



  • Do you invalidate() the layout after adding the widgets? Calling invalidate() on the layout should cause the widget geometries to be calculated. Or did I misunderstand what you are trying to do?



  • I do. Actually I call these now:

    insertPageToLayout: simply inserts the page (a widget) to the _tmpCurrentLayout and calls a show() on the widget as well
    ...
    @insertPageToLayout(indexWhereToPutTheTargetWidget, targetPage);
    targetPage->show();
    targetPage->updateGeometry();
    targetPage->adjustSize();
    _tmpCurrentLayout->update();
    _tmpCurrentLayout->invalidate();
    _tmpCurrentLayout->activate();
    // QRect toGeom = to->geometry();
    QPoint targetPagePosition = targetPage->geometry().topLeft();@

    At this point the targetPagePosition is correct, but if I get the size of the widget here then the size is some smaller size than the widget's size will be. So right after this code I do this:

    @_tmpCurrentLayout->removeWidget(targetPage);
    targetPage->setParent(NULL);
    targetPage->hide(); // without this the page appears as a separate window for a second

    QRect targetPageGeom = QRect(targetPagePosition, targetPage->geometry().size());@
    

    And if I get the size here then that seems to be the real one. So far it works fine.

    NOTE: probably not all of the update/invalidate/... methods should be called, but I just didn't had the time to test the whole program whether if I leave one of them the whole program works fine everywhere. But adjustSize can't be leaved, I'm quite sure about.



  • Hmmm, maybe the parent object's layout needs to be invalidated since that is what provides the size hint to the child layout? If that doesn't work could you prepare a small compileable test case that shows the problem please so that we have some code to look into? The example doesn't need to do the animation just show that the size/pos is wrong after being added to a layout and the layout being invalidated.



  • Actually the _tmpCurrentLayout refers to the parent layout (which will actually contain the targetPage after the transition animation).

    I will prepare an example, but at this time I don't have the time for it. As soon as I can I will give you an example.



  • OK no problem. Whenever you get the time. If I manage to get one put together in the meantime I'll post it here too.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.