2.1 Create using the wizard
Open the Qt Creator screen and select New Project or the Menu bar [File] – [New File or Project] menu item
Bring up the New Project dialog box, select Qt Widgets Application,
Select the [Choose] button, and the following dialog box will pop up
Set the project name and path, follow the wizard to the next step,
Select compilation suite
By default, the wizard adds a class that inherits from CMainWindow, where you can change the class name and base class. The default base class is QMainWindow, QWidget, and QDialog. We can choose QWidget (similar to an empty window). We can create an interface without UI and move on to the next step
By default, we’ll add main. CPP, myWidget. CPP, myWidget. h and a. Pro project file. Click finish to create a Qt desktop application.
Add an empty project
Choose to proceed to the next step. Set the project name and path -> select the compile suite -> Modify the class information -> Finish (same steps as above) to generate an empty project. Add a file to an empty project: Right-click on the project name to bring up the right-click menu and choose “Add New File”.
The New file dialog box is displayed
In this dialog box, select the class or file to add and follow the wizard to complete the file addition.
-
- The pro file
The application generated using the Qt wizard. Pro file format is as follows:
QT += core GUI // contains modules
GreaterThan (QT_MAJOR_VERSION, 4): QT += widgets // contains the widget module only if the version is greaterThan Qt4
TARGET = QtFirst // Application name Name of the generated.exe program
TEMPLATE = app // TEMPLATE type Application TEMPLATE
SOURCES += main.cpp\ // Source file
mywidget.cpp
HEADERS += myWidget.h // header file
Pro is the project file, which is the configuration file that Qmake automatically generates to produce makefiles. The.pro file is written as follows:
- annotation
Start with # and end with this line.
-
The template variable tells Qmake which makefile to generate for the application. Here are the options available: TEMPLATE = app
- App – Creates a makefile for an application. This is the default, so this will be used if the template is not specified.
- Lib – Create a library makefile.
- Vcapp – creates a VisualStudio project file for an application.
- Vclib – create a VisualStudio project file for a library.
- Subdirs – This is a special template that creates a makefile that goes into a specific directory and generates a makefile for a project file and calls make for it.
-
# specify the name of the generated application:
TARGET = QtDemo
- # header files included in the project
HEADERS += include/painter.h
- # UI design files included in the project
FORMS += forms/painter.ui
- # Source files included in the project
SOURCES += sources/main.cpp sources
- # Resource files included in the project
RESOURCES += qrc/painter.qrc
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
The implication of this statement is that if QT_MAJOR_VERSION is greater than 4 (that is, Qt5 and later in use), the widgets module needs to be added. If your project only needs to support Qt5, you can simply add “QT += widgets”. However, to keep your code compatible, it is best to write the statements generated by QtCreator.
- # config information
CONFIG is used to tell Qmake configuration information about the application.
CONFIG += c++11 // use c++11 features
We use “+=” here because we are adding our configuration options to whatever already exists. Doing so is safer than replacing all options already specified with ‘=’.
-
- One of the simplest Qt applications
Main entry function
#include “widget.h”
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Explanation:
-
The standard class name declaration header provided by the Qt system does not have the. H suffix
-
Qt a class corresponds to a header file, and the class name is the header file name
-
QApplication application class
- Manage the control flow and main Settings for graphical user interface applications.
- Is the lifeblood of Qt’s entire background administration. It contains the main event loop, in which all events are processed and scheduled from the windowing system and other resources. It also handles application initialization and termination, and provides conversation management.
- For any GRAPHICAL user interface application that uses Qt, there is exactly one QApplication object, regardless of whether the application has zero, one, two, or more Windows at a time.
-
a.exec()
The program enters a message loop and waits for a response to user input. Here main() passes control to Qt, which does the event processing and returns the value of exec() when the application exits. In exec(), Qt accepts and processes user and system events and passes them to the appropriate widgets.