Qt project types
There are some ways to create a Qt GUI project from scratch. The following project types can be found as in Carbide as in Qt Creator:
- Main window project
- Widget project
- Dialog project
This page aims to clarify what are these ways and when each one should be used.
Qt Main Window
The Qt main window framework is one of the most complete types and offers a framework for building the full application's user interface. If you want to create a full-featured user interface this is the perfect type of Qt project. The framework provides ways to add Tool bar, Status bars, Menu bars, etc. Besides the fact these elements that are optional, a Main Window must have a central widget that is the widget that occupies the center of the screen. The picture bellow shows the disposition of these various elements in a typical application with main window.
More information about the UI elements mentioned above follows:
- Tool bar: A tool bar is a floating widget designed to display controls through which the end user can trigger actions.
- Status bar: It is a horizontal widget that is able to display application status information like 'Ready', 'Waiting', etc.
- Dock widget: They are secondary windows that can be used to display tools and utilities. They can float or be fixed but usually they are next to the central widget.
- Menu bar: It is a visual item that displays menu items that user can trigger and generate requisitions to the application.
To create a Qt Main Window application the developer must inherit from the class QMainWindow (see QMainWindow documentation for more information) class. The code bellow shows the reader file generated by Carbide (Qt Creator generates a very similar file) when creating a Qt Main Window project.
class QtMainWindow : public QMainWindow
QtMainWindow(QWidget *parent = 0);
#endif // QTMAINWINDOW_H
/* .cpp file content */
The Qt Widget project type creates a basic Widget derived from QWidget class. This widget is created without parent widget which makes it a window. Although this window is not derived from QMainWindow class, it can be used as such and the developer may add Tool bar, Status bar and other UI elements to it. This kind of project can be used when the application to be created doesn't use the main aspects of Qt UI style, i.e., when the application is completely custom. In these cases the developer would construct a complete new UI style that doesn't fit with the available Main window framework explained before.
The main class code generated by Carbide Wizard (similar to those created by Qt Creator) are show below:
class QtWidget : public QWidget
QtWidget(QWidget *parent = 0);
#endif // QTWIDGET_H
/* .cpp content */
In a dialog project type the main class is derived from the QDialog class. Usually dialogs are used as secondary windows that are shown to present options and choices to the end user. The Qt framework provides a series of ready to use dialogs. The list follows:
- QColorDialog Dialog widget for specifying colors
- QFileDialog Dialog that allow users to select files or directories
- QFontDialog Dialog widget for selecting a font
- QInputDialog Simple convenience dialog to get a single value from the user
- QMessageBox Modal dialog for informing the user or for asking the user a question and receiving an answer
- QPageSetupDialog Configuration dialog for the page-related options on a printer
- QPrintDialog Dialog for specifying the printer's configuration
- QPrintPreviewDialog Dialog for previewing and configuring page layouts for printer output
- QProgressDialog Feedback on the progress of a slow operation
One use of a dialog as primary window could be a simple configuration application that displays an its settings through a dialog. The main class source code generated by Carbide (very similar to the code generated by Qt Creator) is shown bellow:
class QtDialog : public QDialog
QtDialog(QWidget *parent = 0);
#endif // QTDIALOG_H
/* .cpp file content */