Draw a filled circle with number inside

user3404070

I want to draw a circle once not filled and when user clicks on it, it should be filled. Inside circle I want to write a single digit number.

I tried QPixmap, QLabel and some QPainterPath to make it work, but I wasn't able to do so. How can I do that?

I need circle to be without background, only circle itself with a digit inside. Filling inside of circle will be optional, one time filled with color, one time without. Please advice.

Jeremy Friesner

This is pretty straightforward to do by subclassing QWidget, as shown in the example code below:

#include <QApplication>
#include <QMouseEvent>
#include <QWidget>
#include <QPainter>

class CircleWidget : public QWidget
{
public:
   CircleWidget(int number) : _number(number), _fill(false) {/*empty*/}

   virtual void paintEvent(QPaintEvent * e)
   {
      e->accept();

      QPainter p(this);

      QRect r = rect();
      p.setPen(Qt::black);
      if (_fill) p.setBrush(Qt::green);
      p.drawEllipse(r);

      p.drawText(r, Qt::AlignCenter, QString("%1").arg(_number));
   }

   virtual void mousePressEvent(QMouseEvent * e)
   {
      _fill = !_fill;
      update();  // this will induce another call to paintEvent() ASAP
      e->accept();
   }

private:
   const int _number;
   bool _fill;
};

int main(int argc, char ** argv)
{
   QApplication app(argc, argv);

   CircleWidget cw(5);
   cw.resize(60,60);
   cw.show();
   return app.exec();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Draw Point or filled in circle

From Dev

Draw clockwise filled in circle

From Dev

How to draw a number centered inside a circle with inline svg?

From Dev

How to draw a number inside a circle on Google Map with JavaScript

From Dev

How to draw a number inside a circle on Google Map with JavaScript

From Dev

Draw text inside a circle

From Dev

Draw a transparent circle onto a filled android canvas

From Dev

Draw a circle filled with random color sqares on canvas

From Dev

How to draw stroke inside a circle?

From Dev

How to draw a filled circle on a video frame using matlab

From Dev

Cannot draw a filled circle with SDL2 gfx

From Dev

Trying to draw a filled circle but it has got a black background

From Dev

How do I draw a filled circle using SpriteKit?

From Dev

Draw a filled circle with SDL2_gfx in C/C++

From Dev

How to draw a filled circle on a video frame using matlab

From Dev

svg draw circle with curved text inside

From Dev

How to draw a circle inside a path on canvas in Android

From Dev

Draw with core graphics only inside the circle

From Dev

svg draw circle with curved text inside

From Dev

Draw a rectangle inside a circle with respect to its center

From Dev

How to draw an image inside Rounded Rectangle or inside a shape like circle?

From Java

Maximum number of points that can lie inside the circle

From Dev

How to draw multiple vertical lines inside a semi-circle?

From Dev

How to plot a filled circle?

From Dev

filled / open circle by group

From Dev

Draw a closed and filled contour

From Dev

How do I draw x number of circles around a central circle, starting at the top of the center circle?

From Dev

Drawing a filled circle in a canvas on mouseclick

From Dev

Drawing true filled circle on an image

Related Related

  1. 1

    Draw Point or filled in circle

  2. 2

    Draw clockwise filled in circle

  3. 3

    How to draw a number centered inside a circle with inline svg?

  4. 4

    How to draw a number inside a circle on Google Map with JavaScript

  5. 5

    How to draw a number inside a circle on Google Map with JavaScript

  6. 6

    Draw text inside a circle

  7. 7

    Draw a transparent circle onto a filled android canvas

  8. 8

    Draw a circle filled with random color sqares on canvas

  9. 9

    How to draw stroke inside a circle?

  10. 10

    How to draw a filled circle on a video frame using matlab

  11. 11

    Cannot draw a filled circle with SDL2 gfx

  12. 12

    Trying to draw a filled circle but it has got a black background

  13. 13

    How do I draw a filled circle using SpriteKit?

  14. 14

    Draw a filled circle with SDL2_gfx in C/C++

  15. 15

    How to draw a filled circle on a video frame using matlab

  16. 16

    svg draw circle with curved text inside

  17. 17

    How to draw a circle inside a path on canvas in Android

  18. 18

    Draw with core graphics only inside the circle

  19. 19

    svg draw circle with curved text inside

  20. 20

    Draw a rectangle inside a circle with respect to its center

  21. 21

    How to draw an image inside Rounded Rectangle or inside a shape like circle?

  22. 22

    Maximum number of points that can lie inside the circle

  23. 23

    How to draw multiple vertical lines inside a semi-circle?

  24. 24

    How to plot a filled circle?

  25. 25

    filled / open circle by group

  26. 26

    Draw a closed and filled contour

  27. 27

    How do I draw x number of circles around a central circle, starting at the top of the center circle?

  28. 28

    Drawing a filled circle in a canvas on mouseclick

  29. 29

    Drawing true filled circle on an image

HotTag

Archive