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.setFont(QFont("Arial", 12, 75, false));
        QRectF rectangle(40.0, 2.0, 18.0, 18.0);
        painter.drawText(rectangle, Qt::AlignCenter, "5");

    What am I missing?

  • Lifetime Qt Champion

    Hi and welcome to the forums

    The last line

    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