PDA

View Full Version : The difference between QPinter and QPaintEvent



rezas1000
6th August 2014, 18:12
Hi!
When I use QPainter. Should I use the QPaintEvent with QPainter?
thanks.

stampede
6th August 2014, 19:02
When I use QPainter
When you want to paint something on a paint device:


QImage image("picture.jpg");
QPainter painter(&image);
painter.drawLine(0,0,image.width(),image.height()) ;
// draw a diagonal line on an image

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:


class MyWidget : public QWidget{
Q_OBJECT
protected:
void paintEvent(QPaintEvent * event){
QPainter painter(this);
//... now you can draw something on current widget
}
};

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.