PDA

View Full Version : How to prevent button from expanding to fill layout



Ishmael
23rd May 2010, 18:04
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.


#include <QApplication>
#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QFrame *frame = new QFrame();
frame->setFrameShape(QFrame::Box);
frame->setFrameShadow(QFrame::Plain);

QPushButton *left = new QPushButton("<", frame);
QPushButton *middle = new QPushButton("LongString", frame);
QPushButton *right = new QPushButton(">", frame);

QGridLayout *row_layout = new QGridLayout;
row_layout->addWidget(left,0,0);
row_layout->addWidget(middle,0,1);
row_layout->addWidget(right,0,2);

row_layout->setColumnStretch(0,5); // has no effect
left->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); // has no effect

frame->setLayout(row_layout);
frame->show();

return app.exec();
}

MorrisLiang
23rd May 2010, 18:20
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.

Ishmael
23rd May 2010, 20:01
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?


left->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); // has no effect

MorrisLiang
24th May 2010, 02:58
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.

Coises
24th May 2010, 09:31
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:


#include <QApplication>
#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QFrame *frame = new QFrame();
frame->setFrameShape(QFrame::Box);
frame->setFrameShadow(QFrame::Plain);

QPushButton *left = new QPushButton("<", frame);
QPushButton *middle = new QPushButton("LongString", frame);
QPushButton *right = new QPushButton(">", frame);

QGridLayout *row_layout = new QGridLayout;
row_layout->addWidget(left ,0, 0, Qt::AlignLeft);
row_layout->addWidget(middle,0, 1, Qt::AlignCenter);
row_layout->addWidget(right ,0, 2, Qt::AlignRight);

frame->setLayout(row_layout);
frame->show();

return app.exec();
}

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:


#include <QApplication>
#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QFrame *frame = new QFrame();
frame->setFrameShape(QFrame::Box);
frame->setFrameShadow(QFrame::Plain);

QToolButton *left = new QToolButton(frame);
QToolButton *middle = new QToolButton(frame);
QToolButton *right = new QToolButton(frame);

left ->setToolButtonStyle(Qt::ToolButtonTextOnly);
middle->setToolButtonStyle(Qt::ToolButtonTextOnly);
right ->setToolButtonStyle(Qt::ToolButtonTextOnly);

left ->setText("<");
middle->setText("LongString");
right ->setText(">");

QGridLayout *row_layout = new QGridLayout;
row_layout->addWidget(left , 0, 0, Qt::AlignLeft);
row_layout->addWidget(middle, 0, 1, Qt::AlignCenter);
row_layout->addWidget(right , 0, 2, Qt::AlignRight);

frame->setLayout(row_layout);
frame->show();

return app.exec();
}

Ishmael
24th May 2010, 16:30
"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.

AlekseyK
14th March 2018, 20:25
"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?!