Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Trying to paint on a qpushbutton



  • Hi.

    I am trying to paint a circle using QPainter with a number on a qpushbutton. But I cannot get it to work. As I understand it I need to reimplement paintEvent() in order to do that. Is this necessary or can I do it directly. What I have tried so far is

    void Button::paintEvent(QPaintEvent* event)
    {
        QPainter painter(this);
        painter.setPen(Qt::black);
        painter.setFont(QFont("Arial", 12, 75, false));
        painter.setBrush(Qt::red);
    
        QRectF rectangle(40.0, 2.0, 18.0, 18.0);
        painter.drawText(rectangle, Qt::AlignCenter, "5");
        painter.drawEllipse(rectangle);
    
        QPushButton::paintEvent(event);
    }
    

    What am I missing?


  • Lifetime Qt Champion

    Hi and welcome to the forums

    The last line
    QPushButton::paintEvent(event);

    That will must like clear anything you have drawn before. ( it clears the whole area for the button to draw bg etc)

    Try putting it on top instead and then draw your stuff.



  • That did it. And it makes perfectly sense. Thank you very much.


Log in to reply