Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum size/width of a QPushButton that is created from code

I created 2 rows of push buttons, each row is inside a QHBoxLayout. enter image description here

I create the buttons in the code:

static const char* buttonText = "23456789TJQKA";
for (int ii = 0; buttonText[ii]; ii++)
{
    QPushButton* pushButton = new QPushButton(this);
    pushButton->setText(QString(buttonText[ii]));
    ui->horizontalLayout_1->addWidget(pushButton);
}
for (int ii = 0; buttonText[ii]; ii++)
{
    QPushButton* pushButton = new QPushButton(this);
    pushButton->setText(QString(buttonText[ii]));
    ui->horizontalLayout_2->addWidget(pushButton);
}

The problem is that they can't shrink (when the user resizes the dialog) beyond that size, even though their text would fit in a much smaller width. If I create the buttons manually in the resource editor instead of in the code, they can have smaller width than that.

like image 875
sashoalm Avatar asked Jul 10 '11 03:07

sashoalm


3 Answers

This happens because the minimumSizeHint of the QPushButton does not allow the QLayout to resize it :

The default implementation of minimumSizeHint() returns an invalid size if there is no layout for this widget, and returns the layout's minimum size otherwise. Most built-in widgets reimplement minimumSizeHint().

QLayout will never resize a widget to a size smaller than the minimum size hint unless minimumSize() is set or the size policy is set to QSizePolicy::Ignore. If minimumSize() is set, the minimum size hint will be ignored.

The simple solution is to set the minimum width explicitly:

static const char* buttonText = "23456789TJQKA";
for (int ii = 0; buttonText[ii]; ii++)
{
   QPushButton* pushButton = new QPushButton(this);
   pushButton->setMinimumWidth(5);
   pushButton->setText(QString(buttonText[ii]));
   ui->horizontalLayout_1->addWidget(pushButton);
}
for (int ii = 0; buttonText[ii]; ii++)
{
   QPushButton* pushButton = new QPushButton(this);
   pushButton->setMinimumWidth(5);
   pushButton->setText(QString(buttonText[ii]));
   ui->horizontalLayout_2->addWidget(pushButton);
}
like image 91
pnezis Avatar answered Oct 24 '22 07:10

pnezis


As pnezis wrote, you probably want to override the default minimum size calculated by the button. Here's a way you can do it while avoiding to choose an arbitrary size that might not work when conditions vary (different font or font size, UI style, etc):

QWidget* parent = /* some widget */
auto button = new QPushButton(QLatin1String("X"), parent);
auto textSize = button->fontMetrics().size(Qt::TextShowMnemonic, button->text());
QStyleOptionButton opt;
opt.initFrom(button);
opt.rect.setSize(textSize);
button->setMinimumSize(
  button->style()->sizeFromContents(QStyle::CT_PushButton,
                                    &opt,
                                    textSize,
                                    button));

The above was adapted and simplified from QPushButton's own code. You may want to look at the source of QPushButton::sizeHint for all the details.

like image 20
Paulo Avatar answered Oct 24 '22 07:10

Paulo


setMaximumWidth works for me. sample code is in pyqt, but it should translate directly to C++ without any problems.

from PyQt4 import QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        layout = QtGui.QHBoxLayout()
        texts = [":)",
                 "&Short",
                 "&Longer",
                 "&Different && text",
                 "More && text",
                 "Even longer button text", ]
        for text in texts:
            btn = QtGui.QPushButton(text)
            double = text.count('&&')
            text = text.replace('&', '') + ('&' * double)
            width = btn.fontMetrics().boundingRect(text).width() + 7
            btn.setMaximumWidth(width)
            layout.addWidget(btn)
        self.setLayout(layout)

if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)
    mainWin = Window()
    mainWin.show()
    sys.exit(app.exec_())
like image 1
user178047 Avatar answered Oct 24 '22 06:10

user178047