Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
This article shows one way to think about program architecture when using the Qt library. Because I use this program architecture also when using the Wt library, this article is very similar to Thinking Wt 1: general.
In the first two paragraph I will explain the rationale behind the architecture proposed. The following paragraphs describes the steps in implementing this architecture in a top-down way, followed by a conclusion.
Qt is 'a cross-platform application and UI framework'. I see many resemblances in its architecture with the Wt library, suitable for, among others, web applications. Due to their different running environments, Qt (desktop) and Wt (web) are mutually exclusive. Because I enjoy porting applications to as much platforms as possible, I have developed some applications that use both (but not simultaneously) a Qt and a Wt front-end. To be able to do this easily, I follow the guidelines presented in this article. Additionally, I like to reuse my own widgets and dialogs in multiple applications, which is only possible with a proper architecture.
Note that I follow the same guidelines for Wt front-ends.
The architecture, from biggest to smallest, is: main, Qt::QApplication, dialog, widget:
In this example, main creates a single Qt::WApplication, and creates the first (and only) dialog.
The most basic main function would only call WRun with a createApplication function that only returns a newly created Qt::WApplication:
#include <QtGui/QApplication> |
This code can be created by Qt Creator, by starting a GUI application and instead of using a Qt::QMainWindow called 'MainWindow', use a Qt::QDialog called 'QtDialog'.
signals. In this article I will not create a derived class of Qt::QApplication, but let main create the first dialog, as shown above.
Because a dialog is, well, a dialog, QtDialog is a derived class of Qt::QDialog. QtDialog manages two widgets, but does not respond to their signals.
#ifndef QTDIALOG_H |
#include "qtdialog.h" |
Note that this class must be split in a header (.h) file and an implementation (.cpp) file. Putting both QtDialog's declaration and definition in a single header (.h) file will result in the link error undefined reference to 'vtable for MyDialog'.
Because the pointers m_widget1 and m_widget2 are set to have their parent to 'this' in the QtDialog's constructor, these should not be deleted (doing so results in a double deletion).
A widget is a single visual element. In this example, QtWidget is a button (and thus a derived class of Qt::QPushButton), that displays how often it is clicked.
#ifndef QTWIDGET_H |
#include "qtwidget.h" |
Click 'Build -> Run' or CTRL-R to run the application.
In this article I have shown one of many Qt program architectures you can use, for a very basic application. In my humble opinion, this architecture makes sense, but I am open to discussion on this subject.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.