Violet Giraffe
Violet Giraffe

Reputation: 33579

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

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?

Upvotes: 11

Views: 28243

Answers (3)

Macke
Macke

Reputation: 25680

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

QPushButton { padding: 10px; }

Much better than applying to each subwidget.

Upvotes: 5

Roland Rabien
Roland Rabien

Reputation: 8836

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);

Upvotes: 7

PrisonMonkeys
PrisonMonkeys

Reputation: 1229

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.

Upvotes: 19

Related Questions