Qt - How to control the widget sizes in QLayout

14,571

Solution 1

It's easy. It can be done by giving the stretching factor. That means you have to initiate the value when you use the functions

addWidget(widget,stretchfactor);
       or
addLayout(layout,stretchfactor);

Solution 2

In my opinion, working with stretch factors is not the right solution, here. It's a bit "hackish". Stretch factors are useful e.g. when you have two widgets that both use as much space as possible, but not at a 50:50 rate.

Here, as I see it, you have two widgets (or groups of widgets), where one should only use as much space as it really needs and the other one should take the rest. That is what size policies are for. Set the horizontal policy of the left one to Expanding (or MinimumExpanding if you want to prevent scroll bars) and the right one to Preferred.

Share:
14,571
prabhakaran
Author by

prabhakaran

A newbie to qt,openGL,NetworkProgramming Know python,perl,shell scripting. A well versed(medium level) C++,linux,perl,c# programmer Crawling around javascript,xul and new technologies. I am a Linux lover got tricked into windows.

Updated on June 14, 2022

Comments

  • prabhakaran
    prabhakaran almost 2 years

    I am trying to put some spinboxes,line edits in a layout. But the size extends more than the neccesity. Below is the figure alt text

    Here I am adding a QScrollArea widget,and a QVBoxLayout into a QHBoxLayout. Then I am adding the line edits,spin boxes into the QVBoxLayout. But I want to reduce the width as 2/10 of the total width. Can anybody help me in this?

  • prabhakaran
    prabhakaran over 13 years
    The above picture is not a maximized one, that is it's default size. And I want the widgets should be maximized, when the main widget is maximized . Also The layout's should keep the ratio.
  • Sebastian Negraszus
    Sebastian Negraszus over 13 years
    In my opinion, this is not the best approach. Take a look at my answer.
  • prabhakaran
    prabhakaran over 13 years
    widget only having setSizePolicy. I couldn't find the same for QVBoxLayout
  • Sebastian Negraszus
    Sebastian Negraszus over 13 years
    You can put widgets into another QWidget or QGroupBox
  • Alph0r
    Alph0r about 5 years
    You can also use the setStretch() method of the layout after you added the widgets.