[ fromfile: paintevents.xml id: paintevents ]
A widget should perform the appropriate painting inside its paintEvent()
method.
This is the only place where a QWidget can create a QPainter(this)
.
As you may have observed, a QPaintEvent can be sent to a QWidget due to a number of reasons:
Widget is hidden and then exposed.
Widget is resized, or re-laid out.
update()
or repaint()
is called.
Example 9.19, defines a custom QWidget that overrides paintEvent()
.
Example 9.19. src/widgets/life/lifewidget.h
Steps to get a QPainter for a QWidget:
Example 9.20 shows a paintEvent()
that takes an off-screen QImage and paints it directly
onto the QWidget.
Example 9.20. src/widgets/life/lifewidget.cpp
[ . . . . ] void LifeWidget::paintEvent(QPaintEvent* evt) { QPainter painter(this); if (!m_image.isNull()) painter.drawImage(QPoint(0,0), m_image); }
This program paints successive generations of population maps based on the rules described in Conway's Game of Life. Figure 9.27 shows a snapshot of one generation. In Section 17.2.3 we revisit and parallelize this game.
Normally, paintEvent()
is not called directly, but you can schedule it to be
called, synchronously or asynchronously.
repaint()
does not return until paintEvent()
has
been called.
update()
returns immediately after a QPaintEvent is placed on
the event queue.
Example 9.21 scales the image to the correct
size and saves it, before calling update()
, ensuring that LifeWidget
shows the new image sometime soon.
Example 9.21. src/widgets/life/lifewidget.cpp
[ . . . . ] void LifeWidget::setImage(const QImage& image) { m_size = image.size(); m_image = image.scaled(size()); update(); }
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |