QWidget size policy and QMainWindow size, frames and layouts



  • Hello,

    in a default Qt Desktop application, I set the size policy of the centralWidget to expanding, hoping it would expand to the QMainWindow size. It does not. How can I achieve such expansion?

    Also, layouts take care of their items sizes according to the size policies. A layout does not resize itself according to its items size, or do they? I put a layout inside a QFrame and sized the QFrame hoping the layout takes all the space inside the frame. The QFrame has its size policy set to minimum and its minimum size set to my target size.

    Am I tackling this task right or is a different approach meant to be taken in Qt?


  • Moderators

    You are looking at this bottom-up. Widgets and layouts work from top to bottom, going through parent-child hierarchy.

    In order to make your central widget work as you wish, right click on it in Qt Creator (Qt Designer) and select a desired layout from "Lay out" submenu.



  • The central widget doesn't have a Layout submenu. The QMainWindow has though. Its options "arrange vertically, arrange horizontally, ..." don't appear useful to achieve the expansion.


  • Moderators

    Setting any layout will expand it to fill the parent.



  • Ok, thank you!

    Explicit Layouts are actually never needed.

    Here's how to achieve automatic resizing and custom layouts:

    In Qt Designer:
    in Object View:
    right-click the very top QMainWindow
    select Layout and choose arrange horizontally or vertically

    Now the QWidget below the very top QMainWindow receives an implicit layout.

    in Properties of above mentioned QWidget:
        choose sizePolicy Expanding for both directions to have that QWidget resized automatically when the QMainWindow resizes.
    

    To have widgets inside that QWidget resize automatically set their Properties sizePolicy to Expanding.

    To have a different layout for some objects inside that QWidget from horizontal / vertical, put them inside another QWidget, right-click that QWidget in Object View and select Layout and choose arrange vertically or horizontally. That another QWidget now receives an implicit layout which can be handled like the first QWidget's implicit layout here.

    To have a widget with a fixed width / height from the right / bottom border, set its horizontal / vertical sizePolicy to Fixed, set the minimum width / height and have a widget before that widget with sizePolicy Expanding.


Log in to reply
 

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