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?
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.
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);
You can set the padding for all buttons in a window/widget with:
QPushButton { padding: 10px; }
Much better than applying to each subwidget.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With