Is there a way to enable text wrapping on some simple widgets like QPushButton?

I would like to do a word QPushButton and expand its height instead of expanding the width. How can i do this?

+5
source share
6 answers

I solved the problem with wordwrap on the button as follows:

 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; } 
+2
source

Use QToolButton instead of QPushButton. QToolButton can use multi-line text, and the QToolButton size is easier to control than the QPushButton size.

+5
source

You may be doing something wrong. Buttons should not contain a lot of text, but a few words describing the actions that need to be taken. If you want to make it multi-line, you'd better think of providing QLabel with the appropriate description.

In any case, I do not know any way that supports Qt. The same problem exists, for example, for a QHeaderView signature, where it may be even more applicable. In manual mode, you can always do this by adding the characters "\n" to your header lines (which you can automate for sure).

+2
source

To transfer words to a regular QPushButton, you can implement a proxy class 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 more about the QProxyStyle class in the Qt documentation.

+2
source

There are several solutions using text on an SVG chart. Check it out.

+1
source

QLabel on QPushButton looks good on hover?

0
source

Source: https://habr.com/ru/post/1392428/


All Articles