Qt project types
Introduction[edit | edit source]
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[edit | edit source]
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.
- ifndef QTMAINWINDOW_H
- define QTMAINWINDOW_H
- include <QtGui/QMainWindow>
- include "ui_QtMainWindow.h"
class QtMainWindow : public QMainWindow
{
Q_OBJECT
public:
QtMainWindow(QWidget *parent = 0);
~QtMainWindow();
private:
Ui::QtMainWindow ui;
};
- endif // QTMAINWINDOW_H
/* .cpp file content */
- include "QtMainWindow.h"
QtMainWindow::QtMainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
QtMainWindow::~QtMainWindow()
{
}
Qt Widget[edit | edit source]
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:
- ifndef QTWIDGET_H
- define QTWIDGET_H
- include <QtGui/QWidget>
- include "ui_QtWidget.h"
class QtWidget : public QWidget
{
Q_OBJECT
public:
QtWidget(QWidget *parent = 0);
~QtWidget();
private:
Ui::QtWidget ui;
};
- endif // QTWIDGET_H
/* .cpp content */
- include "QtWidget.h"
QtWidget::QtWidget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
QtWidget::~QtWidget()
{
}
Qt Dialog[edit | edit source]
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:
- ifndef QTDIALOG_H
- define QTDIALOG_H
- include <QtGui/QDialog>
- include "ui_QtDialog.h"
class QtDialog : public QDialog
{
Q_OBJECT
public:
QtDialog(QWidget *parent = 0);
~QtDialog();
private:
Ui::QtDialog ui;
};
- endif // QTDIALOG_H
/* .cpp file content */
- include "QtDialog.h"
QtDialog::QtDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
}
QtDialog::~QtDialog()
{
}