Cause a layout correction without calling show()



  • I'm using Qt 4.5.3 on an embedded system and having some problems. Copy-pasting from my stackoverflow question:

    I'm fixing bugs in some Qt/C++ code. There's a widget that when shown the first time it's layout is wrong, but after being hidden and shown again it's layout is correct. I've scattered some tracing in the code to print the size and position of the widget's elements at various times and I can see that it's the first call to show() that causes the layout to correct itself. What I want to do is trigger the layout correction before showing the widget. I have tried every shot-in-the-dark I can think of. I have called updateGeometry(), adjustSize(), layout()->update(), layout()->invalidate(), layout()->activate(). I have called these on the widget and it's parent widget. Nothing's worked. The only thing that works is calling show().

    So, is there a way to cause a Qt widget to correct it's layout without showing it?

    I can't easily show a screenshot because it's displaying on an embedded system which is not running X. But basically; the widget consists of several labels, they start off with some default values of width and height that causes them all to be smooshed to one side of the widget. I could manually set the positions and sizes to the values I want but I know that I shouldn't need to because Qt corrects the layout perfectly after showing the widget the first time. Also the code that creates the layouts and labels is being autogenerated by Qt's uic from a Qt Designer .ui file.


  • Moderators

    Try this:
    @
    widget->resize(widget->sizeHint());
    widget->show();
    @


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    To add to sierdzio, did you check that you setup the layout properly ?

    On a side note, you can build Qt for Embedded for your workstation with the qvfb output so your can simulate your device screen on your station. That can help debug your design faster



  • Hi all, thanks for the responses.

    bq. Try this:
    @
    widget->resize(widget->sizeHint());
    widget->show();
    @

    Nope, doesn't work :(. Specifically what I try and what I get is:
    @
    printf("%d %d\n", ui.label->width(), ui.label->height()) // prints wrong values
    resize(sizeHint());
    printf("%d %d\n", ui.label->width(), ui.label->height()) // prints the same wrong values
    show();
    printf("%d %d\n" ui.label->width(), ui.label->height()) // prints correct values
    @

    bq. To add to sierdzio, did you check that you setup the layout properly ?

    What exactly do you mean by "setup the layout properly"? I tried calling

    @
    setLayout(ui.layout);
    @

    from the widget's constructor to no effect. The rest of the setup happens in uic-generated code. To expand on that: there's a file, ui_TextPopup.h which is generated by uic from a .ui file. It defines and sets up a main layout which contains the other widgets. Then there's the hand-written code which looks basically like this:

    @
    class TextPopup: QWidget {
    Q_OBJECT

    Ui::TextPopup ui;    
    TextPopup() {
         ui.setupUi(this);
         setLayout(ui.layout);
    }
    
     void showText(QString t) {
         ...
         /* do all sorts of stuff to try and correct the layout */
         ...
         show(); // shows wrong the first time
     }
    

    }
    @


Log in to reply
 

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