Qt Writing Your Own Widgets

Objectives

  • Know when it is better to write a custom widget
  • Be able to re-implement event handler methods
  • Understand when to use signals
  • Be able to decide which internal variables should be made accessible
  • Understand the importance of clean constructors

Custom Widgets

  • Custom widgets can be created by subclassing an existing Qt widget or by subclassing QWidget directly.
  • It is better to write a custom widget when more customization is required.
  • To customizing a Qt widget:
    • Pick a suitable Qt widget
    • Subclass it
    • Reimplement some virtual functions to change its behavior.
  • When none of Qt's widgets are suitable, we can subclass QWidget and reimplement a few event handlers to paint the widget and to respond to mouse clicks.

Re-implementing Event Handler Methods

  • All mouse, keyboard and other events can be re-implemented.
  • Each widget performs all painting operations from within its paintEvent() function.
  • The Scribble example implements a wider set of events.

Signals

  • A signal is emitted when a particular event occurs.
  • Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them.

Clean Constructors

  • Clean constructors tare simple and only take a very small number of argument.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.