Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Trying to paint on a qpushbutton
Qt 6.12 LTS is out! See what's new in the release blog

Trying to paint on a qpushbutton

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jakob Clausen
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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.

      1 Reply Last reply
      3
      • J Offline
        J Offline
        Jakob Clausen
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt
        • Unsolved