Qt - QPushButton text formatting

后端 未结 4 600
余生分开走
余生分开走 2021-01-04 23:26

I have a QPushButton and on that I have a text and and icon. I want to make the text on the button to be bold and red. Looked at other forums, googled and lost my hope. Seem

4条回答
  •  被撕碎了的回忆
    2021-01-04 23:45

    you make the subclass of "QPushbutton", then override the paint event, there you modify the text to your wish.

    here it 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();
    }
    

提交回复
热议问题