Creating custom widgets
Painting with a custom widget is at its heart no different than offscreen painting; all you need is a widget subclass and a painter pointing to the widget, and you're all set. Yet, how do you know when paint?
Qt's QWidget
class defines an interface used by the rendering system to pass events to your widget: Qt defines the QEvent
class to encapsulate the data about an event, and the QWidget
class defines an interface that Qt's rendering system uses to pass events to your widget for processing. Qt uses this event system not just to indicate things like mouse movements and keyboard input, but also for requests to paint the screen as well.
Let's look at painting first. QWidget defines the paintEvent
method, which Qt's rendering system invokes, passing a QPaintEvent
pointer. The QPaintEvent
pointer includes the region that needs to be repainted and the bounding rectangle of the region, because it's often faster to repaint an entire rectangle than a complex region. When you...