UNISA Chatter – Design patterns in C++ Part 4: Widgets … getting started with the QT User Interface

See UNISA – Summary of 2010 Posts for a list of related UNISA posts. Continued from UNISA Chatter – UNISA Chatter – Design patterns in C++ Part 3: Generics & Serializer Pattern  .

QT and Environment Summary

The following is a summary of findings as I worked through the course related book. See the summary post for details on the book.

Terminology Description Example

Class using multiple inheritance, as shown, making it paintable and an object that can have parents and all the other goodies shipped with QObject.


There are different classes of QWidgets:

  • Button
  • Input
  • Display
  • Container
  • QPushButton, QToolButton, QCheckBox, QRadioButton
  • QComboBox, QLineEdit, QTextEdit, QSlider, QTimeEdit, QDateEdit, QSpinButton, QScrollBar
  • QPixMap, QLabel, QProgressBar
  • QMainWindow, QFrame, QToolbar, QTabWidget, QStacketWidget, QFileDialog, QInputDialog, …

QMainWindow is the mother of all windows, the main window, and therefore the parent of all other widgets in the main window. Most QApplication objects, we introduced in previous posts, manage one of these QMainWindow objects.


QSettings is used to save and restore application state, using a persistent map of value and key pairs.


Something that I am pondering over, but actually liking, is the apparent overlap and similarity with Win32 and .NET classes. The QMessageBox just brought back so many memories … it is used, as in the other mentioned environments, to popup a standard dialog.

QMessageBox::question(this,”if it is neither here nor there, where is it?”, “interesting”, 0, –1);
Images and Resources

To include binary files, such as images, in our Qt application we follow a few easy steps:

  1. Create a Resource Collection File (.qrc) that is an XML file that lists all of the binary files.
  2. Generate a resource file (rcc) using a Python script
  3. Add a RESOURCE line in the project file that defines the name of the qresource file.

… or we can simply use the IDE.

See the sample solution for examples.


To control the layout of the windows we can use the following classes, amongst others:

  • Layout (must have a QWidget or QLayout parent)
    • QVBoxLayout
    • QHBoxLayout
    • QGridLayout
    • QStackedLayout
  • Spacing (fixed number of pixles)
    • addSpacing
  • Stretch (stretchable number of pixles)
    • addStretch
  • Structs (minimum size of perpendicular dimension)
    • addStruct
  • Regions
    • QDockWidget
    • QSplitters

See the sample solution for examples.

Design Pattern Summary

Terminology Description
Pattern: Monostate
  • Creational design pattern
  • Allows multiple instances of an application to share the same one (mono) state.
  • It is an implementation of the Singleton pattern.
    image … the Visual Studio class diagram emerges 🙂 Sanity prevails.
Pattern: Command
  • Behavioural design pattern
  • Encapsulates commands/operations  in as objects with a common interface, making it possible to operations, log operations and implement features such as undo, redo and transactions.

The will post the sample solution I developed as soon as I submit the assignment, which use most of the QT features summarised above. When I am done with UNISA, I will probably rewrite all of these samples using Visual Studio 2010 and demonstrate the phenomenal features and productivity of that environment as well.

Here are a few screen shots, which should highlight the favourite sample program emerging through the technology forest once again:

image … 8! = 512 … it even works 🙂

I once had lots of fun writing the code and debugging the gremlins and odd behaviours using the QT IDE.  Not sure what the problem is, but there are often locked files, especially after a debug session, forcing an IDE restart … brings back memories from very early Visual Studio builds and can be rather tiring late at night.

Next post will explore the QVC pattern … a pattern that has also settled firmly in Visual Studio 2010 samples. If only we could complete these assignments using Visual Studio 2010 😐

Comments (2)

  1. JLC says:

    Hi Willy, may I ask why you say that the Monostate pattern is a creational pattern rather than a structural pattern? I am might be very wrong (and then you will hopefully correct me), but the monostate pattern prescribes that all data members be static – meaning that the pattern describes something about the object's structure. Where object_state + object_methods = object_structure; thus altering state, means altering structure, thus structuaral pattern?  Thanks for posting your notes!

  2. I slotted the MonoState into the creational category due to its similiarities with the Singletion pattern, which is also a creational pattern.

Skip to main content