Results 1 to 9 of 9

Thread: Widget size in layouts with different screen resolution problem [S60]

  1. #1
    Join Date
    Sep 2009
    Location
    Kraków, Poland
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Widget size in layouts with different screen resolution problem [S60]

    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)
    Qt Code:
    1. Example::Example(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4.  
    5. setStyleSheet("QLabel {border: 2px solid green;}"); // just to make labels size visible
    6.  
    7. QVBoxLayout *mainLayout = new QVBoxLayout(this);
    8.  
    9. QLabel* label1 = new QLabel("Label 1");
    10. QLabel* label2 = new QLabel("Label 2");
    11. QLabel* label3 = new QLabel("Label 3");
    12.  
    13. label1->setMaximumHeight(120);
    14. label2->setMaximumHeight(120);
    15. label3->setMaximumHeight(120);
    16.  
    17. mainLayout->addWidget(label1);
    18. mainLayout->addWidget(label2);
    19. mainLayout->addWidget(label3);
    20.  
    21. label1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
    22. label2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
    23. label3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
    24.  
    25. setLayout(mainLayout);
    26. }
    To copy to clipboard, switch view to plain text mode 

    And screenshots:
    1) This is how it should look on smaller display (widgets are smaller then max size, they fill the whole screen)
    smaller..jpg
    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
    bigger..jpg
    3) This is what i managed to achieve (widgets have the right size but they are not top aligned)
    good_size_bad_po&#115.jpg

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

  2. #2
    Join Date
    Jun 2010
    Location
    India
    Posts
    50
    Thanks
    1
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Widget size in layouts with different screen resolution problem [S60]

    Hi..

    See if this work...?

    Qt Code:
    1. mainLayout->addWidget(label2);
    2. mainLayout->addWidget(label3);
    3.  
    4. //case 2:
    5. mainLayout->addItem(new QSpacerItem(10,10,QSizePolicy::Expanding,QSizePolicy::Expanding);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Widget size in layouts with different screen resolution problem [S60]

    As agathiyaa said you can use QSpacerItem.

    Or you can do this:
    Qt Code:
    1. //...
    2. mainLayout->addWidget(label3);
    3.  
    4. mainLayout->addStretch();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    Kraków, Poland
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Widget size in layouts with different screen resolution problem [S60]

    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?

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Widget size in layouts with different screen resolution problem [S60]

    I'm not sure that i really understand your problem, but have you tried MinimumExpanding:
    Qt Code:
    1. label1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
    2. //...
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Sep 2009
    Location
    Kraków, Poland
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Widget size in layouts with different screen resolution problem [S60]

    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

  7. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Widget size in layouts with different screen resolution problem [S60]

    You can edit the policies for labels to make them expand "larger" then the spacer (I think i finally understood your problem )
    Qt Code:
    1. //...
    2. label1->setMaximumHeight(120);
    3. label2->setMaximumHeight(120);
    4. label3->setMaximumHeight(120);
    5.  
    6. QSizePolicy policy = label1->sizePolicy();
    7. policy.setVerticalStretch(3); //don't remember the exact "formula" but 3 should be enough
    8. label1->setSizePolicy(policy);
    9.  
    10. label2->setSizePolicy(policy);
    11.  
    12. label3->setSizePolicy(policy);
    13.  
    14.  
    15. QSpacerItem *spacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
    16.  
    17. mainLayout->addWidget(label1);
    18. mainLayout->addWidget(label2);
    19. mainLayout->addWidget(label3);
    20. mainLayout->addSpacerItem(spacer);
    21.  
    22. //don't set sizepolicy again... if you want other then prefered set it before policy edit
    23. //label1->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    24. //label2->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    25. //label3->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    26. //...
    To copy to clipboard, switch view to plain text mode 
    Last edited by Zlatomir; 20th June 2010 at 13:19.

  8. The following user says thank you to Zlatomir for this useful post:

    JacquesBaniaque (20th June 2010)

  9. #8
    Join Date
    Sep 2009
    Location
    Kraków, Poland
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Widget size in layouts with different screen resolution problem [S60]

    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

  10. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Widget size in layouts with different screen resolution problem [S60]

    This is the line that does the "magic"
    Qt Code:
    1. policy.setVerticalStretch(3);
    To copy to clipboard, switch view to plain text mode 
    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)
    Last edited by Zlatomir; 20th June 2010 at 20:50.

Similar Threads

  1. How can i make widget automatically adjust screen size
    By kapoorsudhish in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2009, 15:21
  2. changing screen resolution of touchscreen
    By aj2903 in forum Qt for Embedded and Mobile
    Replies: 7
    Last Post: 8th April 2009, 18:57
  3. Replies: 1
    Last Post: 27th March 2008, 15:10
  4. Screen resolution
    By sabeesh in forum Qt Programming
    Replies: 4
    Last Post: 7th September 2007, 17:00
  5. Screen Resolution
    By vijay anandh in forum Qt Programming
    Replies: 3
    Last Post: 6th October 2006, 13:47

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.