Qt - QPushButton text formatting

I have a QPushButton and I have text and icon. I want the text on the button to be bold and red. I looked at other forums on Google and lost hope. There seems to be no way to do this if the button has an icon (of course, unless you create a new icon, which is text + the old icon). Is this the only way? Anyone have a better idea?

+6
c ++ formatting qt button
source share
3 answers

You really do not need to subclass change the formatting of your button, but use style sheets, for example.

QPushButton { font-size: 18pt; font-weight: bold; color: #ff0000; } 

Applying this to the button you want to change, make the button text 18pt bold and red. You can apply via widget->setStyleSheet()

Applying this to the widget in the hierarchy above, all the buttons below will stylize, the QT stylesheet mechanism is very flexible and quite well documented.

You can also set style sheets in the designer, this will lead to a change in the widget that you are currently editing.

+17
source share

you create a subclass of "QPushbutton", then override the drawing event, there you will change the text as you wish.

here he is,

 class button : public QPushButton { Q_OBJECT public: button(QWidget *parent = 0) { } ~button() { } void paintEvent(QPaintEvent *p2) { QPushButton::paintEvent(p2); QPainter paint(this); paint.save(); QFont sub(QApplication::font()); sub.setPointSize(sub.pointSize() + 7); paint.setFont(sub); paint.drawText(QPoint(300,300),"Hi"); paint.restore(); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); button b1; b1.showMaximized(); return a.exec(); } 
+5
source share

You can subclass QLabel and completely throw away all of its mouse events (so that they reach the parent). Then enter QPushButton, set the layout on it and in this layout enter QLabel with rich text. You get a button that contains QLabel and is clickable. (Any Qt widget can have a layout, including ones you never expected they could.)

+2
source share

All Articles