Results 1 to 2 of 2

Thread: Understanding QLayout, Alignment and (maybe) SizePolicy

  1. #1
    Join Date
    Feb 2021
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question Understanding QLayout, Alignment and (maybe) SizePolicy

    Hi all,

    so i'm not actually new to Qt, yet I still cannot understand how QLayout, Alignment and Sizepolicy work in combination. Building a complex Layout always results in rage and trial&error.

    Its pretty clear that e.g. i will create a QWidget and a QHBoxLayout. To the Layout i will add two PushButtons.
    At this point, the button widgets share the layouts width 50:50

    1.jpg

    Changing the buttons Sizepolicy has no effect (probably because the layout[or the button widget itself?] will hintsize here to share the whole size of the layout's widget between the button widgets?).
    As soon as I set any Alignment, the button will have a decreased width. I have no idea where that new size is coming from. I thought it might be based on the buttons text but it isnt. SizePolicy still has no effect.

    2.jpg

    I truly dont understand or find any documentation on this behavior.
    I can only guess that Qts default hintSize is calculating different for whatever condition.

    Can somebody provide a link to a documentation or explain, how those 3 layout objects work together?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Understanding QLayout, Alignment and (maybe) SizePolicy

    The layout is the only thing that actually sizes the widgets. The layout does that by combining the constraints it has (i.e. the client area of the widget it is attached to) with the demands and suggestions of the widgets that have been added to the layout. The child widgets have a size policy and size hint that express preferences about the sizing of the widget that the layout can use.

    In the case of a default button the size policy is Preferred in the horizontal direction and Fixed in the vertical direction. The size hint is driven by the button size needed for the label. In the vertical direction the layout has not resized the buttons to fill the available space precisely because of their size policy. The button horizontal size policy allows for any size, and the layout has stretched them to fit the space available in that direction. The split of space is equal because the two buttons were added to the layout with equal (default) stretch factors.

    If you specify alignment of one of the buttons then you are directing the layout to place the widget within the space available to it rather than sizing it to fit. The widget in that case assumes its sizeHint() and is placed, top/bottom left/right etc. of the available space as directed. The space available to each widget does not change with alignment. See the example code and result below (Button 1 and 3 are the same size regardless of the alignment of Button 4)

    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QPushButton>
    4. #include <QHBoxLayout>
    5. #include <QVBoxLayout>
    6.  
    7. int main(int argc, char **argv) {
    8. QApplication app(argc, argv);
    9.  
    10.  
    11. // Default
    12. QHBoxLayout *layout1= new QHBoxLayout();
    13. layout1->addWidget(new QPushButton("Button 1"));
    14. layout1->addWidget(new QPushButton("Button 2"));
    15.  
    16.  
    17. // Alignment used
    18. QHBoxLayout *layout2= new QHBoxLayout();
    19. layout2->addWidget(new QPushButton("Button 3"));
    20. layout2->addWidget(new QPushButton("Button 4"), 0, Qt::AlignHCenter);
    21.  
    22. QVBoxLayout *layout= new QVBoxLayout(&w);
    23. layout->addLayout(layout1);
    24. layout->addLayout(layout2);
    25.  
    26. w.show();
    27. w.resize(800, 150);
    28.  
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 
    Screenshot_20210411_122444.jpg

    Your images show a change in size of the left button: without your code I cannot tell you why that is.

Similar Threads

  1. SizePolicy Help
    By NIteLordz in forum Qt Programming
    Replies: 1
    Last Post: 7th March 2015, 09:35
  2. Nested Layouts and sizePolicy
    By pagapov in forum Newbie
    Replies: 13
    Last Post: 24th October 2010, 10:29
  3. layouts, sizePolicy, sizeHint,
    By Kronen in forum Newbie
    Replies: 2
    Last Post: 5th November 2009, 18:43
  4. The use of SizePolicy
    By Placido Currò in forum Newbie
    Replies: 2
    Last Post: 9th April 2009, 23:10
  5. QLayout alignment stretch
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 27th October 2007, 21:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.