GUI#
Introduction#
File
-> New project
-> Qt Widgets Application
.Window structure#
QMainWindow
. If created thanks to the designer, a .h, a .cpp and a .ui files will be created.QWidget
and allows for a menu to be displayed :
Modality#
NonModal : the window does not block the inputs from its parent (will act as a separate window)
WindowModal : the window block inputs for its parent window, parent window’s sibling window, the parent window of the parent window and the parent window’s sibling window
ApplicationModal : blocks all other windows inputs (e.g. used for an input dialog)
Designer Window creation#
To modify the main window or create a subsidiary one (to be used as widget for example), right-click on your ui
folder and select Add New ...
-> Qt
-> Graphical Interface Class Qt Designer
.
You then have access to multiple pre-defined widgets.
Start by adding one layout (drag and drop), e.g.
Horizontal Layout
.The layout will be floating on the Window. To set a flaw to your window, right click on it ->
Lay out the page
-> select a layout, e.g.Vertical Layout
.You can then add widgets as you wish. Use the different layouts to organize your window.
On the right window, you will see its tree structure and can modify data of your widgets.

Expanding
.layoutStretch
to select which percentage each widget takes on screen. Those are weights for each column/raw of your layout.
Window not updating after modification#
Clean
then rebuild.Callbacks#
Go to slot ...
and double click the signal you are interested in.
Promoting a Widget#
Promote to ...
.PATH
, just give the relative path to it and check Global Header
; else, it’s relative path to the root of the project).Add
, select it and click Promote
.
Custom Widget#
Widget
on your window and promote it to your custom, widget-derived class. 1// In callback from the menu click ...
2
3// Create About window, and gives the mainwindow as parent -> is WindowModal and linked to it
4About myaboutwindow(&mainwindow);
5// Execute it
6w.exec();
7// The exec will lock until window is closed
8
9// ------------------------------------------------
10
11// With no parent, calling exec will make the widget ApplicationModal
12About myaboutwindow(nullptr);
13w.exec();
14
15// -------------------------------------------------
16
17// As further method will return immediately, if object is non-static -> will be destroyed even before being shown
18static About myaboutwindow(nullptr);
19w.show(); // returns immediately, making the window NonModal
Note
Using a static variable here may not be the best option, as it is very locally managed and could not be, e.g., closed by the program controller.
If you are looking to “hot-swap” one of your widget (e.g. you placed a widget somewhere in your window, that should show options based on a currently selected object), the following code can help :
1// From the ui.cpp file, passing a QWidget* new_widg
2
3// Recover your old widget
4QWidget* old_widg = this->ui->mywidget;
5// Prepare new widget
6new_widg->setParent(this->ui->layoutContainingOldWidget);
7new_widg->setObjectName(QString::fromUtf8("mywidget));
8// Replace
9this->ui->layoutContainingOldWidget->replaceWidget(this->ui->mywidget, new_widg);
10this->ui->mywidget = widg;
11// Cleanup
12if(old_widg){delete old_widg;}
Note
Replacing with another flaw may disregard the layout stretch values, hiding the widget from the view.
Input dialogs#
Qt offers multiple input dialogs to get values, strings, files … from the user :
1// Get a double
2bool ok;
3double value = QInputDialog::getDouble(&parent, "Title of window", "Window text", 1 /*value*/, 0 /*min value*/, 2 /*max value*/, 3 /*decimals*/, &ok);
4if(ok)
5{
6 // do something with the vlue
7}
8
9// ---------------------------------------------
10
11// Int
12int value = QInputDialog::getInt(&parent, "Title of window", "Window text", 0 /*value*/, -500 /*min*/, 500 /*max*/, 1 /*step*/,&ok);
13
14// ---------------------------------------------
15
16// Text (QInputDialog::getMultiLineText also possible)
17QString str = QInputDialog::getText(&parent, "Title of window", "Window text", QLineEdit::Normal, "Default", &ok);
18std::string str_std = str.toStdString();
19const char* str_c = str_std.c_str();
20
21// ---------------------------------------------
22
23// Items list
24QStringList items;
25items << "Option 1" << "Option 2" << "Option 3";
26QString item = QInputDialog::getItem(&parent, "Title of window", "Window text", items, 0 /*base selected*/, false /*if can edit items*/, &ok);
27
28// ---------------------------------------------
29
30// File picker
31QString filepath = QFileDialog::getOpenFileName(&parent, "Title", "/" /*default dir, make it exec current*/, "FileFormat1 (*.txt); Fileformat2 (*.yaml)");
32
33// ---------------------------------------------
34
35// File saver
36QString filepath = QFileDialog::getSaveFileName(&parent, "Title", "/" /*default dir, make it exec current*/, "Yaml (*.yaml); Text (*.txt)");
37if(filepath != "")
38{
39 // do something
40}

Information window#
You can quickly show message boxes to inform the user about what is happening in your program (an error occurred, confirm discarding current file …) :
1// You can check the clicked button by its retval
2int retval;
3 // Error box
4retval = QMessageBox::critical(&parent, "Critical", "Test Message");
5 // Information box
6retval = QMessageBox::information(&parent, "Information", "Test Message");
7 // Ask user with yes/no
8retval = QMessageBox::question(&parent, "Question", "Test Message");
9 // Warning box
10retval = QMessageBox::warning(&parent, "Warning", "Test Message");
11
12// The following allows to show simple about box (but better to create it through your own widget !)
13QMessageBox::about(&parent, "About", "Test Message");
14 // Qt About window - MUST BE present for Qt Open Source programs
15QMessageBox::aboutQt(&parent, "About Qt");

The displayed buttons can be modified, an extra text can be displayed on user click … See Qt - QMessageBox class.

Controlling the application#
Factory
pattern along with its Controller
.Factory#
Factory
pattern allows you to create your object and dispatch the needed references easily.- Create your objects -> called the
init
phase All the used objects are stocked inside the factory, mainly the
Controller
- Create your objects -> called the
- Initialize the relations -> called the
build
phase Setup the relations where X should know Y but Y should know X too
- Initialize the relations -> called the
- Run the program -> called the
run
phase Mainly, it will just call your
Controller
run method
- Run the program -> called the
main
function should only contains those three calls. In Qt, it will be ended by the return app.exec()
.MVC-lite#
Core
from your GUI
, and looks like :
Note
Controller
. You only need to update its model.mywindow_ui.h
files (considered as your Models) to recover their elements and implement a subsidiary way to register callbacks to inform any View when a data is modified.Controller
class which will call modifications on your UI data (Model) while registering callbacks from the user gestures.Controller
will hold all data required to do the tasks (your Core
system), and update your UI data directly by calling specific functions in mywindow.h.