When you want to paint something on a paint device:
painter.drawLine(0,0,image.width(),image.height());
// draw a diagonal line on an image
QImage image("picture.jpg");
QPainter painter(&image);
painter.drawLine(0,0,image.width(),image.height());
// draw a diagonal line on an image
To copy to clipboard, switch view to plain text mode
QPainter can be used on images, printers and widgets (and few other things derived from QPaintDevice).
QPaintEvent is an event - it is sent to a widget that needs to update itself. It contains some parameters related to the area being repainted;
Typically you reimplement a "paintEvent" protected method in a class derived from QWidget:
Q_OBJECT
protected:
//... now you can draw something on current widget
}
};
class MyWidget : public QWidget{
Q_OBJECT
protected:
void paintEvent(QPaintEvent * event){
QPainter painter(this);
//... now you can draw something on current widget
}
};
To copy to clipboard, switch view to plain text mode
When it comes to QWidgets, QPainter can be used only inside of paintEvent() method.
The difference between QPinter and QPaintEvent
QPainter is an object used to draw stuff on various paint devices (not necessarily widgets), whereas QPaintEvent is an event related to rendering on QWidgets and Qt event system.
Bookmarks