Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Understanding Widget sizing



  • I'm learning Qt and try to understand how widget sizing is working.
    I have made a simple layout including a Gaphics view and a Button as shown below:
    bc32c9c4-3041-42e6-a142-b8d3c240275e-image.png

    Both widgets are placed in a vertical layout and the sizes in the Designer are as follows:
    960a8219-faa5-470b-a476-4645fdabeeec-image.png
    2589925b-f893-48ca-8d5b-33e28a7b7b42-image.png

    So w=576 and h=393 for the Graphics view and w=576 and h=28 for the button.

    For testing I have added the following code to the constructor:
    12516f0f-d40a-49fd-82e2-9acf5055dfb8-image.png

    My first question is why is sizeHint written in italics and size in normal?

    Running the program provides the following outputs:
    Graphics view size: QSize(100, 30)
    Button size: QSize(100, 30)
    Graphics view size hint: QSize(256, 192)
    Button size hint: QSize(93, 28)

    None of these numbers are matching with the numbers in the designer, why? Where are these numbers comming from?

    Then, I have reimplemented the resizeEvent function and copied the qDebug lines in it.
    ffa9cdc4-d186-4737-afc0-ed3324460bae-image.png

    Now the outputs are as follows:
    648d647c-8816-4467-bd3b-c2442aa5251f-image.png

    Now the sizes match with those from the Designer but why the sizehint of the Graphics view has changed and the size hint of the button has not?

    When changing the size of the window with the mouse the minimum size is limited to the following numbers.
    d334e734-a93f-4082-a44c-54b9d3e7e7f3-image.png

    Again, where are these numbers comming from and why is the window limited to this size?

    Is there a place in the documentation where these behaviors are described in depth?





  • @Bert59
    To answer just why the numbers are not what you expect in the constructor. In a word, sizes only mean anything after a widget is actually shown. That's why, for example, they are correct in resizeEvent(). Before .show() they seem to be almost anything, don't rely on them.




Log in to reply