Getting Started Programming with Qt Widgets▲
In this topic, we teach basic Qt knowledge by implementing a simple Notepad application using C++ and the Qt Widgets module. The application is a small text editor which allows you to create a text file, save it, print it, or reopen and edit it again. You can also set the font to be used.

Running the Example▲
To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.
Creating the Notepad Project▲
Setting up a new project in Qt Creator is aided by a wizard that guides you step-by-step through the project creation process. The wizard prompts you to enter the settings needed for that particular type of project and creates the project for you.
The UI text in Qt Creator and the contents of the generated files depend on the Qt Creator version that you use.
To create the Notepad project, select File > New Project > Application (Qt) > Qt Widgets Application > Choose, and follow the instructions of the wizard. In the Class Information dialog, type Notepad as the class name and select QMainWindow as the base class.
The Qt Widgets Application wizard creates a project that contains a main source file and a set of files that specify a user interface (Notepad widget):
-
CMakeLists.txt - the project file.
-
main.cpp - the main source file for the application.
-
notepad.cpp - the source file of the notepad class of the Notepad widget.
-
notepad.h - the header file of the notepad class for the Notepad widget.
-
notepad.ui - the UI form for the Notepad widget.
The files come with the necessary boiler plate code for you to be able to build and run the project. We will take a closer look at the file contents in the following sections.
Learn More
About |
Here |
---|---|
Using Qt Creator |
|
Creating other kind of applications with Qt Creator |
Main Source File▲
The wizard generates the following code in the main.cpp file:
#include
"notepad.h"
#include <QApplication>
int
main(int
argc, char
*
argv[])
{
QApplication a(argc, argv);
Notepad w;
w.show();
return
a.exec();
}
We will go through the code line by line. The following lines include the header files for the Notepad widget and QApplication. All Qt classes have a header file named after them.
#include
"notepad.h"
#include <QApplication>
The following line defines the main function that is the entry point for all C and C++ based applications:
int
main(int
argc, char
*
argv[])