The custom control has been implemented in the previous introduction, and the widget is placed in the main interface to draw a circle. For details, refer to "QT custom window"
The following describes how to set the paint brush color and the fill color of the image.
Paint color:
Void circlewidget: paintevent (qpaintevent * event) {qpainter painter (this); // painter. setbrush (qbrush (qcolor (0x00, 0xff, 0x00); // painter. drawellipse (qpoint (100,100), 100,100); qcolor Green (0, 0xff, 0); // set the color qpen pen (green); // define the paint brush painter. setpen (PEN); painter. drawrect (100,100, 50, 30 );}
You can also set the line width and style.
Void circlewidget: paintevent (qpaintevent * event) {qpainter painter (this); // painter. setbrush (qbrush (qcolor (0x00, 0xff, 0x00); // painter. drawellipse (qpoint (100,100), 100,100); qcolor Green (0, 0xff, 0); // set the color qpen pen (green); // define the pen. setwidth (5); // pen. setstyle (QT: dashdotdotline); painter. setpen (PEN); painter. drawrect (100,100, 50, 30 );}
Fill color:
void CircleWidget::paintEvent(QPaintEvent *event){ QPainter painter(this); QBrush brush(QColor(0x00,0xFF,0x00)); painter.setBrush(brush); //painter.drawEllipse(QPoint(100,100),100,100); painter.drawRect(100,100,50,30);}
To change the canvas color, you can first obtain the length and width of the canvas, draw a rectangle, and then add the rectangle color as the desired color.
void CircleWidget::paintEvent(QPaintEvent *event){ QPainter painter(this); int width=this->width(); int height=this->height(); QBrush brush(QColor(0xFF,0xFF,0xFF)); painter.setBrush(brush); painter.drawRect(0,0,width,height);}
How to set the pen color and fill when draw a line in QT