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 ...

Get Application Development with Qt Creator - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.