Head file
protected:
void paintEvent(QPaintEvent *); //drawing operation will be done here
CPP file
void Widget::paintEvent(QPaintEvent *QPaintEvent)
{
QPainter painter(this); //create a QPainter object, this means painter devices is component of widget
// using drawLine to draw a line start from (0,0) to end of (100,100)
painter.drawLine(QPoint(0,0), QPoint(100,100));
// drawing a rectangle, outline is red, fulfilled with green
// setting up art of pen and brush.
QPen pen;
pen.setColor(QColor(255,0,0)); // red pen
QBrush brush(QColor(0,255,0,125)); // green of brush and 50% of transparent
painter.setPen(pen); // add the pen
painter.setBrush(brush); // add the brush
painter.drawRect(50,50,100,100); // draw a rectangle
}
References
Qt5.9中QPainter类用法 https://blog.csdn.net/naibozhuan3744/article/details/79038084