Results 1 to 5 of 5

Thread: Facing problem in setting shrink behaviour of widgets in QGridLayout

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Facing problem in setting shrink behaviour of widgets in QGridLayout

    Hi,

    I'm facing difficulties in setting QSizePolicy to my widgets in QGridLayout.
    I'm creating a custom time-selection widget, that has 2 QTextEdit, 4 buttons. My problem is the QTextEdit widgets and QPushButtons are not shrinking as desired.
    PFB, the screenshot of the Timeselection Widget that I'm trying to develop -

    This is max horizontal shrink
    TimeSelection Widget.png

    This is max vertical shrink
    TimeSelection_Vertical Resize limit.png

    Desired behaviour -
    I need the horizontal shrink to be limited to text in QTextEdit without wrapping text(displaying in next line). And I need the vertical size to shrink upto row-end of the text.

    Here the buttons are also not shrinking beyond the one I showed in 1st image. I need the buttons to be as small as possible.

    As shown in the pic below, I need the exact idea to be translated to my widgets behavior.
    Desired_TimeSelection.png

    Qt Code:
    1. CTimeSelect::CTimeSelect(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. this->setLayout(&lyt);
    5. this->setContentsMargins(0, 0, 0, 0);
    6. lyt.setSpacing(0);
    7. lyt.setContentsMargins(0, 0, 0, 0);
    8. onInit();
    9. }
    10.  
    11. CTimeSelect::~CTimeSelect()
    12. {
    13. }
    14.  
    15. void CTimeSelect::onInit()
    16. {
    17. timeDisp.setText("11:30");
    18.  
    19. timeDisp.setFont(QFont("Lucida Console", 24, QFont::Bold, false));
    20. timeDisp.setReadOnly(true);
    21. timeDisp.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    22. timeDisp.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    23. timeDisp.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    24.  
    25. timeUp.setText("+");
    26. timeUp.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    27. timeDown.setText("-");
    28. timeDown.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    29.  
    30. am_pm.setText("PM");
    31. am_pm.setReadOnly(true);
    32. am_pm.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    33. am_pm.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    34. am_pm.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    35.  
    36. am.setText("^");
    37. am.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    38. pm.setText("v");
    39. pm.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    40.  
    41. lyt.addWidget(&timeDisp, 0, 0, 2, 4);
    42. lyt.addWidget(&timeUp, 0, 4, 1, 1);
    43. lyt.addWidget(&timeDown, 1, 4, 1, 1);
    44. lyt.addWidget(&am_pm, 0, 5, 2, 2);
    45. lyt.addWidget(&am, 0, 7, 1, 1);
    46. lyt.addWidget(&pm, 1, 7, 1, 1);
    47.  
    48. lyt.setColumnStretch(0, 1);
    49. lyt.setColumnStretch(4, 0);
    50. lyt.setColumnStretch(5, 1);
    51. lyt.setColumnStretch(7, 0);
    52.  
    53. }
    To copy to clipboard, switch view to plain text mode 

    Please help me solve this issue. Thank you.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Facing problem in setting shrink behaviour of widgets in QGridLayout

    - Use QLineEdit instead of QTextEdit, to avoid multipleline and text wrapping
    - Set minimum size of all the widgets as required.

    BTW do you want only to limit the minimum size, or you also want the widgets to be always at it's minimum size and never expand?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Facing problem in setting shrink behaviour of widgets in QGridLayout

    I want the widgets to limit to minimum size. It should expand or shrink according to it's parent behavior.

    I've used QTextEdit, now it's shrinking beyond it's minimum font size used in the display.
    Untitled.png

    Though I set 1 column & 1 row span for buttons, it's not square shaped. How do I make it square-shaped using QGridLayout and without setting size?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Facing problem in setting shrink behaviour of widgets in QGridLayout

    Though I set 1 column & 1 row span for buttons, it's not square shaped. How do I make it square-shaped using QGridLayout and without setting size?
    There are two ways
    1. Set the size policy as Fixed, (widget will never change there size ever, not even if parent expands)
    2. Set the size policy as Expanding, and set the minimum size limit. (this way widgets can take any size between the minimum limit size to largest possible)

    Try this

    Qt Code:
    1. void CTimeSelect::onInit()
    2. {
    3. timeDisp.setText("11:30");
    4.  
    5. timeDisp.setFont(QFont("Lucida Console", 24, QFont::Bold, false));
    6. timeDisp.setReadOnly(true);
    7. timeDisp.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    8. timeDisp.setMinimumWidth(100);
    9.  
    10. timeUp.setText("+");
    11. timeUp.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    12. timeUp.setMaximumWidth(25);
    13.  
    14. timeDown.setText("-");
    15. timeDown.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    16. timeDown.setMaximumWidth(25);
    17.  
    18. am_pm.setText("PM");
    19. am_pm.setReadOnly(true);
    20. am_pm.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    21. am_pm.setMaximumWidth(25);
    22.  
    23. am.setText("^");
    24. am.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    25. am.setMaximumWidth(25);
    26. pm.setText("v");
    27. pm.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    28. pm.setMaximumWidth(25);
    29.  
    30. lyt.addWidget(&timeDisp, 0, 0, 2, 1);
    31. lyt.addWidget(&timeUp, 0, 1, 1, 1);
    32. lyt.addWidget(&timeDown, 1, 1, 1, 1);
    33. lyt.addWidget(&am_pm, 0, 2, 2, 1);
    34. lyt.addWidget(&am, 0, 3, 1, 1);
    35. lyt.addWidget(&pm, 1, 3, 1, 1);
    36. }
    To copy to clipboard, switch view to plain text mode 

    I repeat use QLineEdit
    Last edited by Santosh Reddy; 18th April 2013 at 11:49.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    rawfool (18th April 2013)

  6. #5
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Facing problem in setting shrink behaviour of widgets in QGridLayout

    Sorry I used QLineEdit after your first reply. In my reply it was a typo.
    And before you posted the code, I did the same way like yours. Now its looking the way I wanted.
    Thank you very much.

Similar Threads

  1. Replies: 2
    Last Post: 16th December 2010, 11:52
  2. Replies: 1
    Last Post: 13th August 2010, 06:28
  3. Strange QGridLayout behaviour
    By Mat12345 in forum Qt Programming
    Replies: 0
    Last Post: 7th November 2009, 10:48
  4. qgridlayout setting in qt designer
    By hottdogg in forum Qt Tools
    Replies: 1
    Last Post: 4th October 2009, 07:58
  5. Setting behaviour in designer
    By Morea in forum Qt Tools
    Replies: 3
    Last Post: 19th March 2006, 15:33

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.