#include <QApplication> #include <qlabel>int main (int argc, char *argv[]) {qapplication app (argc, argv); Qlabel *label = new Qlabel ("Hello qt!"); Label->show (); return app.exec ();}
Lines 1th and 2nd contain the definitions of two classes: Qapplication and Qlabel. For each QT class, there will be a header file with the same name, and the header file contains the definition of the class. Therefore, if you use a class object in your program, you must include the header file in your program.
Line 3rd is the entrance to the program. In almost all cases where QT is used, the main () function only needs to perform some initialization before handing control over to the QT library, and then the QT Library passes events to inform the user of the behavior of the program. ARGC is the number of command-line variables, and argv is an array of command-line variables. This is a C/s + + feature. It's not QT-specific, and QT needs to deal with these variables anyway.
Line 5th defines a Qapplication object app. Qapplication manages a wide range of resources for a wide variety of applications, such as default fonts and cursors. The creation of the APP requires ARGC and argv because QT supports some of its own command-line parameters. A Qapplication object must be used in every application that uses QT, and it is necessary to create this object before any QT Windows system parts are used. The APP is created here and handles subsequent command-line variables (such as-display in the X window). Note that all command-line parameters that are recognized by QT are removed from argv (and ARGC is reduced as a result).
Line 6th creates a Qlabel widget to display "hello,qt!". In Qt and Unix terminology, a widget is a visible element in the user interface, which is equivalent to "container" plus "controller" in Windows terminology. buttons, menus, scroll bars (scroll bars), and frames are all examples of widgets. A window part can contain other widgets. For example, an application interface is usually a window that contains Qmenubar, some Qtoolbar, a qstatusbar and some other widgets. Most applications use a Qmainwindow or a qdialog as the program interface, but QT allows any widget to be a window. In this example, the Qlabel widget is used as the main application window.
The 7th exercise we created Qlabel visible. When a widget is created, it is always hidden and must be called show () to make it visible. With this feature we can customize these widgets before displaying them so that there is no flicker.
The 8th line is that main () gives control to Qt. Here, the program enters the event loop. The event loop is a stand-by pattern that waits for the user's actions (such as pressing the mouse or the keyboard). The user's actions will produce events that the program can react to (also known as "messages"). The program's response to these events is usually to perform one or several functions.
For the sake of simplicity, we did not delete the Qlabel object by calling delete at the end of the main () function. This memory leak is harmless, because a small program like this, at the end of the operating system will release the memory heap that the program occupies.
Qt command Prompt Create a project step from the command line:
Source--Project---makefile-> executable program
Let's compile this program. Create a directory called Hello, in the directory to create a C + + source file named Hello.cpp, write the above code into the file.
Open Qt Command Prompt;
In command-line mode, switch the directory to Hello, and then enter the command:qmake–project. This command will produce an engineering document (HELLO.PRO) that relies on the work platform;
Re-enter the command:qmake hello.pro. This command generates a makefile that can be used on a specific work platform through a project file;
Last input command: make to generate the application;
To run this program, you can get the following program interface.
[email protected] Command prompt to create a project from the command line