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

Problem with managing size of the widgets, layouts etc.



  • Hi,
    I have a problem, that is probably easy to solve but somehow I'm lacking positive results. I don't use UI, this is a QWidget and windows 10 with mingw64.

    My program has a following structure:

    1. horizontal layout:
      11. vertical layout
      111. 2 buttons + e.g. spacer
      12. vertical layout:
      121. tableview object
      122. horizontal layout
      1221. 2 buttons

    When I use mouse to resize window, it resizes normally but 111 buttons move accordingly to the height of the window. What I want is that thos buttons stay in their place and ONLY spacer is resizing (imagine a columne, where vert_space_field1=button1, vert_space_field2=butt2, vert_space_field3=spacer and when resizing vertically, only vert_space_field3 resizes).

    Thanks for your help :)!



  • @mimickxd Hi, Can you show your code ? How do you add your "spacer" ?

    This should do the job:

    vLayout11->addWidget(bt1);
    vLayout11->addWidget(bt2);
    vLayout11->addStretch();
    


  • You are actually correct, sir, it works.

    Thank you very much but can you actually explain why does it work?
    I mean, I already read doc. about streching and spacing but couldn't figure that one out :).


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The stretch is a spacer item that will expand as much as possible with respect to the other widgets in the same layout. If you want to ensure it takes all the place it can, then you should pass a bigger stretch factor than the other widgets in the layout.


Log in to reply