Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add some padding between QPushButton’s boundary and its inner text?

Tags:

qt

Longer strings don’t look too good in a QPushButton, because there’s no spacing between the text and button’s frame, i. e. the text is placed tightly within the button. Is there a way to add some padding?

like image 623
Violet Giraffe Avatar asked Mar 06 '13 12:03

Violet Giraffe


3 Answers

You can set the padding of a QPushButton via its stylesheet.

myButton->setStyleSheet("padding: 3px;");

Or

myButton->setStyleSheet("padding-left: 5px; padding-right: 3px;"
"padding-top: 1px; padding-bottom: 1px;");

More information on stylesheets can be found here.

like image 193
PrisonMonkeys Avatar answered Nov 19 '22 02:11

PrisonMonkeys


Rather than having to set a stylesheet for each button, I found it easier to update the sytle so that the minimum size for each button is a little bigger. You can subclass QProxyStyle which is the easiest way to modify styles, since it will apply changes to whatever style is selected ie QWindowsXPStyle, QWindowsVistaStyle, QMacStyle etc.

Override sizeFromContents like this to make the minimum size of buttons a little bigger:

class ProxyStyle : public QProxyStyle
{
public:
    QSize sizeFromContents(ContentsType ct, const QStyleOption* opt, const QSize & csz, const QWidget* widget = 0) const
    {
        QSize sz = QProxyStyle::sizeFromContents(ct, opt, csz, widget);
        if (ct == CT_PushButton)
            sz.rwidth() += 20;

        return sz;
    }
};

And then after you create your application, but before you create your first window call:

a.setStyle(new ProxyStyle);
like image 6
Roland Rabien Avatar answered Nov 19 '22 01:11

Roland Rabien


You can set the padding for all buttons in a window/widget with:

QPushButton { padding: 10px; }

Much better than applying to each subwidget.

like image 4
Macke Avatar answered Nov 19 '22 01:11

Macke