Qt Widgets Application Project Structure

Qt widgets application project structure in terms of files and C++ objects. How Qt widget applications are organized. How to get pointers to widgets in Qt.

In this part of the Qt creator C++ tutorial, we look at the organization of default Qt Widgets Application projects. These projects are created in Qt Creator using the New Project button. They are then selected from the New Project wizard dialog box. Most projects in this tutorial series are based on Qt Widgets Application projects started in Qt Creator.

Part 10 of the Qt Creator C++ Tutorial

Go to the Qt Programming Tutorial Series Table of Contents

File Structure of a Qt Widgets Application Project

To follow along with this explanation, start Qt Creator and then create a new project. We have done this in previous parts of this tutorial series, but here are the steps anyway.

Start a New Project in Qt Creator

Start a new project in Qt Creator. To do this, click the New Project button on the default page in Qt Creator. Alternatively select File → New File or Project… from the top menu.

In the New Project dialog box that opens, click Qt Widgets Application to select it. Click the Choose… button.

In the next dialog box, use the Browse… button to navigate to the desired destination folder. Create a new folder there for the project called proj_struct. Name the project proj_struct.

Click the Next button in all the dialog boxes, leaving the default options. In the final dialog box, click the Finish button.

New Project Folders

By creating a new folder in the previous section, the project now resides in a folder called proj_struct. After finishing with the steps to build a new project, a new folder, also called proj_struct, is created in the initial proj_struct folder.

New Project Folder Created by Qt Creator for a Qt Widgets Application
New Project Folder Created by Qt Creator for a Qt Widgets Application

Default Files

The second proj_struct folder contains the files shown in the image below.

Default Files in a New Qt Creator Qt Widgets Application Project
Default Files in a New Qt Creator Qt Widgets Application Project

The files shown in the above image have the following purpose.

  • main.cpp – contains the main() function of the application
  • mainwindow.cpp – code for main application window
  • mainwindow.h – header file for above
  • mainwindow.ui – XML file for main window GUI. Visually editable in design mode in Qt Creator.
  • proj_struct.pro – text file that is the main Qt Creator project file
  • proj_struct.pro.user – XML file that contains user setting for the project

All of the above files can be opened directly in Qt Creator using the left pane when in Edit mode, except for the user settings XML file. This is shown in the image below.

Project Files in Qt Creator for a Qt Widgets Application
Project Files in Qt Creator for a Qt Widgets Application

Folders and Files after Building

After building the project in Qt Creator, extra folders and files are generated.

Debug Folder Generated after Build in Qt Creator
Debug Folder Generated after Build in Qt Creator

Because the default settings in Qt Creator are for a debug version of the project, the new folder is called build-proj_struct-Desktop-Debug. If the settings are changed to release, then a new folder called build-proj_struct-Desktop-Release is generated.

Debug Build Files Generated by Qt Creator
Debug Build Files Generated by Qt Creator
Qt User Interface Compiler

When the build process is started, the Qt User Interface Compiler is invoked. This compiler reads in the mainwindow.ui XML file and generates the ui_mainwindow.h file.

Qt Meta Object Compiler

The Qt Meta Object Compiler (moc) is also invoked as part of the build process. It reads in the mainwindow.h file and generates the moc_mainwindow.cpp file. This step is necessary to implement the signal slot mechanism and other Qt specific details. A file containing definitions, called moc_predefs.h is also added to the project.

Other Generated Files

A make file is also automatically generated. Three object files are generated from building the C++ source files. Finally the executable application file is produced as the final part of the build process.

Object Structure of a Qt Widgets Application Project

By default, a Qt Widgets Application project created in Qt Creator consists of three main objects. A QApplication object called a and a MainWindow object called w are instantiated in main.cpp in the main() function. MainWindow is derived from the QtMainWindow class.

The third object is as mentioned above is an automatically generated Ui_MainWindow class that is then put into the Ui namespace and effectively renamed to MainWindow.

The image below shows the three main objects of a project.

Main Objects in a Qt Widgets Application Project in Qt Creator
Main Objects in a Qt Widgets Application Project in Qt Creator

MainWindow Class

The MainWindow class opens up the GUI to us so that we can programmatically modify it. It allows signals and slots to be implemented by placing widgets on the main window.

Ui::MainWindow Class

Ui::MainWindow is generated from mainwindow.ui by the Qt User Interface Compiler. It is saved in the ui_mainwindow.h header file.

Ui::MainWindow is the Ui_MainWindow class that is effectively renamed at the bottom of the ui_mainwindow.h file by the following lines of code, which also place it in the Ui namespace.

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

How to Get Pointers to Widgets in Qt

Widgets placed in the main window in Qt creator are easily accessed from within the MainWindow class. This is done by referencing the ui pointer that is a private member of the MainWindow class. We did this before, for example in part 5 of this series – Qt 5 Hello World Tutorial using Qt Creator.

Code from that project is shown in the following listing.

void MainWindow::on_btnHello_clicked()
    static int count = 0;
    QString strCount;

    // display text message in first label
    ui->lblHello->setText("Hello, world!");

    // display button clicked count in second label
    strCount.sprintf("%d", count);

Two labels are accessed in the above code using the ui pointer. In the first instance:

ui->lblHello->setText("Hello, world!");

The label lblHello is accessed from ui. The setText() method or member function of the label is then called to change the label text.

References for this Qt Widgets Application Tutorial

Qt widgets example on the Qt website.

Explanation 1 on Stack Overflow.

Explanation 2 on Stack Overflow.

Namespace question on Qt forum.

Leave a Reply

Your email address will not be published. Required fields are marked *