Nominate our 2022 Qt Champions!

I could use some guidelines on layouts

  • Hi -

    I'm to the point in my (first) Qt project where I actually care about the appearance of the UI (!) so it's time for me to become more familiar with layouts. I read a few of the pages on them, but I'd like some input from the mavens here.

    1. When a widget is placed into a layout, many of its properties are locked. So, do I correctly gather that the right development workflow is to get your widgets the way you want them, then finally add them? This seems a little inconvenient, as you don't really know what something's going to look like until you put it in a layout. How does one minimize the number of times you put something into a layout, then break the layout to modify the widget to look right?

    2. I read that layouts can contain other layouts. Should there be a master layout for the entire app, or is it better practice to adhere to a 1:1 window:master-layout pattern?

    3. does anyone know of a good reference for best practices? Now that I'm actually getting started on this, I'd like to avoid bad habits up front.

    I realize that there aren't really definitive answers to these questions, but I've always learned from other people's input in these forums. Thanks.

  • Lifetime Qt Champion



    Which one do you mean ? The geometry of the widget is controlled by the layout.
    Other than that any property can still be set?
    After you insert widget into a layout u control how big it gets by min/max values and
    a policy about if it should take all space or stay as small as possible etc.

    Im not sure what a master layout would be.
    You might use nested layouts when you need the an item that stack the other way or
    a multi section widget.

    Best Pratice i can think of is use them from start. Design with them from start.
    its hard to fix up later. And they are needed for correct scaling on different screen.

  • @mrjj

    1. from the layout page:

    All QWidget subclasses can use layouts to manage their children. The QWidget::setLayout() function applies a layout to a widget. When a layout is set on a widget in this way, it takes charge of the following tasks:

    Positioning of child widgets.
    Sensible default sizes for windows.
    Sensible minimum sizes for windows.
    Resize handling.
    Automatic updates when contents change:
    Font size, text or other contents of child widgets.
    Hiding or showing a child widget.
    Removal of child widgets.

    I also notice that I'm unable to resize widgets in design mode when the widgets are in a layout.
    2. by "master layout" what I mean was one layout for the entire application (no matter how many displays it may have), within which are subordinate layouts for each window. I'm pretty sure you can do this; I'm more interested in whether one should do it.
    3. Thank you. I intend to use them. As I mentioned, I want to do this the right way the first time. I just don't entirely know what the right way is.

  • Lifetime Qt Champion

    @mzimmers said in I could use some guidelines on layouts:

    Well you can still change font type etc. byt yes, x,y is locked.

    I also notice that I'm unable to resize widgets in design mode when the widgets are in a layout.

    You set minimum size or max size to express such wishes. you can also set fixed size to not let it
    take more space. So you can change size but its other way than when not in layout.

Log in to reply