Results 1 to 7 of 7

Thread: How to prevent button from expanding to fill layout

  1. #1
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default How to prevent button from expanding to fill layout

    I have several buttons in a row, but I do not want them to fill the available space. I've tried every manner of SizePolicy, Stretch, Spacers, etc. but the only way I have managed to restrict the width of the buttons is by setting MaximumSize, which unfortunately is not portable (on Mac it clips the button). There must be some easy way of saying "make the button as small as possible without clipping any text or clipping the button itself". Here's some sample code. Any suggestions would be very welcome.

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QFrame *frame = new QFrame();
    9. frame->setFrameShape(QFrame::Box);
    10. frame->setFrameShadow(QFrame::Plain);
    11.  
    12. QPushButton *left = new QPushButton("<", frame);
    13. QPushButton *middle = new QPushButton("LongString", frame);
    14. QPushButton *right = new QPushButton(">", frame);
    15.  
    16. QGridLayout *row_layout = new QGridLayout;
    17. row_layout->addWidget(left,0,0);
    18. row_layout->addWidget(middle,0,1);
    19. row_layout->addWidget(right,0,2);
    20.  
    21. row_layout->setColumnStretch(0,5); // has no effect
    22. left->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); // has no effect
    23.  
    24. frame->setLayout(row_layout);
    25. frame->show();
    26.  
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to prevent button from expanding to fill layout

    set the buttons' sizepolicy to maximum.Then they'll not expand to fill.You might also need a QSpacerItem,if you don't want them to be layout in the center.
    It's not the goodbye that hurts,but the flashback that follow.

  3. #3
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to prevent button from expanding to fill layout

    Thanks for the response. I tried that but nothing happened. In the code I posted above, I changed this line, with no effect. What am I missing?

    Qt Code:
    1. left->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); // has no effect
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to prevent button from expanding to fill layout

    Sorry,I didn't quite read through your question.
    So you want the button to be as small as possible,and don't want to use setMaximunSize.
    I guess you have to subclass QPushbutton,and override the sizeHint() method.
    Last edited by MorrisLiang; 24th May 2010 at 04:05.
    It's not the goodbye that hurts,but the flashback that follow.

  5. #5
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to prevent button from expanding to fill layout

    Quote Originally Posted by Ishmael View Post
    There must be some easy way of saying "make the button as small as possible without clipping any text or clipping the button itself".
    Try:
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QFrame *frame = new QFrame();
    9. frame->setFrameShape(QFrame::Box);
    10. frame->setFrameShadow(QFrame::Plain);
    11.  
    12. QPushButton *left = new QPushButton("<", frame);
    13. QPushButton *middle = new QPushButton("LongString", frame);
    14. QPushButton *right = new QPushButton(">", frame);
    15.  
    16. QGridLayout *row_layout = new QGridLayout;
    17. row_layout->addWidget(left ,0, 0, Qt::AlignLeft);
    18. row_layout->addWidget(middle,0, 1, Qt::AlignCenter);
    19. row_layout->addWidget(right ,0, 2, Qt::AlignRight);
    20.  
    21. frame->setLayout(row_layout);
    22. frame->show();
    23.  
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 
    or, if you do not want the current style’s minimum width for push buttons (which is why the left and right buttons don’t shrink in the above) enforced:
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QFrame *frame = new QFrame();
    9. frame->setFrameShape(QFrame::Box);
    10. frame->setFrameShadow(QFrame::Plain);
    11.  
    12. QToolButton *left = new QToolButton(frame);
    13. QToolButton *middle = new QToolButton(frame);
    14. QToolButton *right = new QToolButton(frame);
    15.  
    16. left ->setToolButtonStyle(Qt::ToolButtonTextOnly);
    17. middle->setToolButtonStyle(Qt::ToolButtonTextOnly);
    18. right ->setToolButtonStyle(Qt::ToolButtonTextOnly);
    19.  
    20. left ->setText("<");
    21. middle->setText("LongString");
    22. right ->setText(">");
    23.  
    24. QGridLayout *row_layout = new QGridLayout;
    25. row_layout->addWidget(left , 0, 0, Qt::AlignLeft);
    26. row_layout->addWidget(middle, 0, 1, Qt::AlignCenter);
    27. row_layout->addWidget(right , 0, 2, Qt::AlignRight);
    28.  
    29. frame->setLayout(row_layout);
    30. frame->show();
    31.  
    32. return app.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Coises for this useful post:

    Ishmael (24th May 2010)

  7. #6
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to prevent button from expanding to fill layout

    "setToolButtonStyle" does the trick! Thanks a lot!

    Another solution I found is to create an icon image for the button and get rid of the text altogether. Then the buttons resize to fit the icon.
    Last edited by Ishmael; 24th May 2010 at 20:33.

  8. #7
    Join Date
    Nov 2010
    Posts
    47
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to prevent button from expanding to fill layout

    Quote Originally Posted by Ishmael View Post
    "setToolButtonStyle" does the trick! Thanks a lot!

    Another solution I found is to create an icon image for the button and get rid of the text altogether. Then the buttons resize to fit the icon.
    And what value You'd set if setToolButtonStyle() - either does not help me. I have only icon on a button, no text - 2 tool buttons with same sizes, both with 22x22 icons, and minimum sizes set to 24x24. However when I add them to 2 different layouts: 1st increased to 30x24, 2nd - to 30x29. How to prevent them resizing and keep same sizes for both?!

Similar Threads

  1. How to expanding the layout?
    By Kode.Cooper in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 15:24
  2. Replies: 1
    Last Post: 3rd August 2009, 22:01
  3. Replies: 6
    Last Post: 18th December 2008, 22:16
  4. Can I insert expanding layout in Qt Designer?
    By zolookas in forum Newbie
    Replies: 2
    Last Post: 6th October 2008, 19:17
  5. Button in Layout help
    By IsleWitch in forum Newbie
    Replies: 4
    Last Post: 9th October 2007, 17:59

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.