Is there a way to enable word wrapping of text on some simple widgets like QPushButton?
Use a QToolButton instead of a QPushButton. QToolButton can use multiline text and QToolButton's size is easier to control than the size of a QPushButton.
I have solved the problem with wordwrap on a button this way:
QPushButton *createQPushButtonWithWordWrap(QWidget *parent, const QString &text)
{
auto btn = new QPushButton(parent);
auto label = new QLabel(text,btn);
label->setWordWrap(true);
auto layout = new QHBoxLayout(btn);
layout->addWidget(label,0,Qt::AlignCenter);
return btn;
}
For word wrapping in regular QPushButton you can implement proxy style class derived from QProxyStyle.
/**
proxy style for text wrapping in pushbutton
*/
class QtPushButtonStyleProxy : public QProxyStyle
{
public:
/**
Default constructor.
*/
QtPushButtonStyleProxy()
: QProxyStyle()
{
}
virtual void drawItemText(QPainter *painter, const QRect &rect,
int flags, const QPalette &pal, bool enabled,
const QString &text, QPalette::ColorRole textRole) const
{
flags |= Qt::TextWordWrap;
QProxyStyle::drawItemText(painter, rect, flags, pal, enabled, text, textRole);
}
private:
Q_DISABLE_COPY(QtPushButtonStyleProxy)
};
And later in your own MyQtPushButton:
MyQtPushButton::MyQtPushButton()
: QPushButton()
{
setStyle(new QtPushButtonStyleProxy());
}
See additional information on QProxyStyle class in Qt documentation.