tags:

views:

109

answers:

5
+1  Q: 

Qt layout problem.

I have a following Qt code:

QVBoxLayout* box = new QVBoxLayout;
label = new QLabel(); // will be set later dynamically
box->addWidget (label);

Text in label will be set later. The problem is that when label resizes, it resizes QVBoxLayout, and it resizes other neighboring widgets. I don't want to make a label or layout fixed width. Because I want them to resize with a whole window.

Is it possible to tell a widget to take all the place that it has in a layout, but not more?

A: 

You can either set a maximumSize or have a look at sizePolicy.

Exa
As I said, I don't want to set a maximum size. I took a deep long look at size policy earlier, but don't know how to use it.
Łukasz Lew
A: 

I think you need to set the stretch factor (setStrechFactor) for the main window you are placing the layout.

alisami
Good idea. I setStretchFactor of box and other widgets in main layout to 0.But no effect :(
Łukasz Lew
setting the stretch factor to 0 means that the layout stretch is auto calculated according to the other widgets. for example if you have a pushbutton and a label in a layout and set the stretch factor to 0 for each element, then the size for each widget is auto assigned. if you set 100;1; as a stretch factor, there will be a 100 proportion for the widgets' sizes.
alisami
A: 

Hey,

Maybe you could have a look at the following methods :

- void setScaledContents(bool)
- void setWordWrap(bool on)

I hope it fits your requirements !

Andy M
A: 

Have you tried to modify the size policy of widgets? You can accomplish what you want with this.

Here are all size policies: http://qt.nokia.com/doc/4.6/qsizepolicy.html#Policy-enum

Patrice Bernassola
Which policy accomplishes what I want?
Łukasz Lew
Use the QSizePolicy::Maximum policy and set the sizeHint of your label to the layout space size. So the label will be limited to the space it has in the layout.
Patrice Bernassola
A: 

Łukasz, do I understand you correctly that you want the label to fill the space of one line all the time? If that is so, if the text is just one line, then the solution would simply be:

QVBoxLayout* box = new QVBoxLayout;
label = new QLabel(" "); // The space will create a vertical space in the layout
box->addWidget (label);

The "empty" label will now behave exactly as it will after you set the text.

But I must confess, I'm not sure I truely understand your question. Can you be more specific or maybe provide a screenshot of what you're trying to accomplish?

Robin