PDA

View Full Version : Widget size in layouts with different screen resolution problem [S60]



JacquesBaniaque
19th June 2010, 14:11
Hi,
i'll try to explain what i'm trying (without satisfing results) to achive though it might not be easy.

Generally i want to put few widgets in a layout (say QVBoxLayout) that will be displayed with different sizes and alignment on S60 phones with different vertical pixels count (640px and 320px)
Lets say that ideal vertical size of all my widgets (sum of their ideal height) is 360px. So as you can see it is smaller thetn 640 and greater then 320.

When displayed on smaller display i want widgets to scale verticaly to fit whole screen (320px) but when on the bigger display i want them to have their maximum (360px) size AND aligned on top (so the unused space is at the bottom of the screen).

This is more or less the code i use (it was used to make third screenshot)


Example::Example(QWidget *parent) :
QWidget(parent)
{

setStyleSheet("QLabel {border: 2px solid green;}"); // just to make labels size visible

QVBoxLayout *mainLayout = new QVBoxLayout(this);

QLabel* label1 = new QLabel("Label 1");
QLabel* label2 = new QLabel("Label 2");
QLabel* label3 = new QLabel("Label 3");

label1->setMaximumHeight(120);
label2->setMaximumHeight(120);
label3->setMaximumHeight(120);

mainLayout->addWidget(label1);
mainLayout->addWidget(label2);
mainLayout->addWidget(label3);

label1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
label2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
label3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);

setLayout(mainLayout);
}


And screenshots:
1) This is how it should look on smaller display (widgets are smaller then max size, they fill the whole screen)
4807
2) This is how it should look on bigger display (widgets have their maximum size, empty space at the bottom)
NOTE: this was achieved by setting stretch for every label and inserting stretch at the end - so it only looks like something i want to achieve, in fact it isn't
4806
3) This is what i managed to achieve (widgets have the right size but they are not top aligned)
4805

As far as i can remember it is my first post here so please be patient if i did not include some important info :)

agathiyaa
19th June 2010, 20:06
Hi..

See if this work...?



mainLayout->addWidget(label2);
mainLayout->addWidget(label3);

//case 2:
mainLayout->addItem(new QSpacerItem(10,10,QSizePolicy::Expanding,QSizePoli cy::Expanding);

Zlatomir
19th June 2010, 20:37
As agathiyaa said you can use QSpacerItem.

Or you can do this:


//...
mainLayout->addWidget(label3);

mainLayout->addStretch();

JacquesBaniaque
19th June 2010, 23:45
I afraid both hints are not the case.
Spacer with expanding policy is 'space hungry' and it squeezes label widgets to their minimum sizes (not sure to which size actually, minimumHintSize?). The 'addStretch' method behaves the same.
So even if i would set minimum size to exactly fill the smaller screen (which would be rather 'workaround way', what if there would be third phone with let say 240 vertical size?) it won't use this bit of extra space on bigger screen.

The way You propose might be good but You would have to give me more hints about size policies/size hints/min-max sizes/something else? i should set. I really tried many combinations and none of them did work.
Maybe i should somehow control the size of container widget?

Zlatomir
20th June 2010, 01:06
I'm not sure that i really understand your problem, but have you tried MinimumExpanding:


label1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
//...

JacquesBaniaque
20th June 2010, 10:35
Nope, the last one does the same effect as picture 3)
AFAIK Expanding and MinimumExpanding does not differ very much (later one sets a minimum size at minimumSizeHint(?)).

I'll try once again:
Labels should have their maximum size if possible. By maximum size i dont mean 'expand to infinity/screen size' but expand to the maximum size i set (3x120 in the example code).
So if labels vertical size is 360 and display vertical size is 300 all 3 labels will have 100 v-size and will fill whole screen idealy. But if display is bigger then 360 v-size then labels should reach their maximum size (3x120) and be aligned at the top of the screen (like in picture 2) and the empty, unused space should be at the bottom.

And like i said earlier - do not be suggested by the code i posted. Setting maximum size of widgets this way or assigning policies to them might not be the good solution.

C'mon, somebody had to the have the same problem someday :)

Zlatomir
20th June 2010, 13:44
You can edit the policies for labels to make them expand "larger" then the spacer (I think i finally understood your problem ;) )


//...
label1->setMaximumHeight(120);
label2->setMaximumHeight(120);
label3->setMaximumHeight(120);

QSizePolicy policy = label1->sizePolicy();
policy.setVerticalStretch(3); //don't remember the exact "formula" but 3 should be enough
label1->setSizePolicy(policy);

label2->setSizePolicy(policy);

label3->setSizePolicy(policy);


QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);

mainLayout->addWidget(label1);
mainLayout->addWidget(label2);
mainLayout->addWidget(label3);
mainLayout->addSpacerItem(spacer);

//don't set sizepolicy again... if you want other then prefered set it before policy edit
//label1->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
//label2->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
//label3->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
//...

JacquesBaniaque
20th June 2010, 19:40
Yes! That's it! Problem solved!
Thanks Zlatomir :)

Still i would really appreciate if You explained why this one worked and previous did not or at least direct me to some source were i could read about it

Zlatomir
20th June 2010, 20:15
This is the line that does the "magic"

policy.setVerticalStretch(3);
That widgets that will widget->setSizePolicy(policy); will have a 3 times bigger stretch factor (which control the relative size of the widgets in a container dialog/window/widget/etc)

And in our case all the widgets will occupy more vertical space in container (but no more that 120, because you set up that way), and this will prevent the spacer to expand more (kind of like the widgets/labels have "priority" to expand and the spacer expands in the remaining space if the labels reach the maximum size)