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.
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.
Comments