Before writing the code, you need to set up the environment, for details, see: "Qt 5.4.2 Ubuntu Environment Building".
a simple blank window
Open terminal, new file through Vim first_qt.cpp, because QT code is C + + code, so the new file suffix is. cpp.
The code reads as follows:
#include <QApplication> #include <qwidget>int main (int argc, char *argv[]) {qapplication app (argc, argv);// Initialize Qwidget w;w.show (); App.exec (); Main Event loop return 0;}
after writing the code, we need to compile this code into an executable program through the g++ compiler (c + + compiler), but since this code uses a function that is not a C + + standard library, but a QT library, it is cumbersome to compile with the QT library on which it depends. We can use the Qmake tool provided by QT to compile the QT source code .
directory where Qmake is located:
To configure the environment variable, path adds the Qmake path:
1) Copy the directory where the Qmake is located
2) Add the following code in the final position of the ~/.BASHRC or /etc/profile script (/home/mike/qt5.4.2/5.4/gcc_64/bin is the directory where qmake Different environments):
Export Path=/home/mike/qt5.4.2/5.4/gcc_64/bin: $PATH
3) Run the script file and let the environment variable set to take effect immediately (source ~/.BASHRC), which qmake to see if the setting is successful:
Next, we will automatically generate the project file through the Qmake project, then generate the makefile through the project file, and then compile the source code into an executable program by make parsing makefile instructions:
1) generate the required engineering documents:Qmake-project
The Qmake tool automatically generates a project file with the suffix. Pro based on the current directory source code, with the same file name as the current directory name.
Open the project file with the following contents:
Simple analysis of Engineering documents:
1)Template: What templates are used to create the target file.
app to create a makefile file for building a
qt application ;
Lib to create the Makefile file for the application library .
2)target: Describes the name of the target file (that is, the executable file name). Default =.pro file name = project directory name.
3)Includepath: Describes the path of the header file that the compiler needs to search for when compiling the project.
4)#: Comment Code
5)HEADERS: Tell the compiler, the. h file path and its file file ( not used here ).
6)SOURCES: Tells the compiler, the source code (. cpp file ) file path and its file name.
7)FORMS: Add Designer-generated. ui files ( not used here ).
This project file lacks a very important thing: the addition of modules. Qt 5 defines a number of modules, each containing relatively independent library files and implement their own functions, if not add the corresponding module in the project file, even if the code is written correctly, compile will also prompt many functions or classes are undefined . Here, we write the code mainly related to the graphical interface, it is necessary to add Qtwidgets module in the project file:QT + = Widgets.
2) Generate makefile with qmake command according to the project file:qmake
3) Compile the source code using the generated makefile : Make
4) Run executable program:./QT
Introduction to the basic framework of QT code
We will basically see this one framework when we write QT code later:
1) contains the required header files : Usually and used in the same name, Qt's class name usually begins with ' Q '.
2) Initialize : Create a Qapplication object and pass the user input parameters to it, each QT GUI application has only one Qapplication object, and is defined before other objects are defined ( That is, usually the first sentence in main () is usually: Qapplication app (argc, argv);). It is primarily used to manage the control flow of QT GUI applications and the main setup options that are required for each QT GUI application.
3) Main Event loop : App.exec () is the function to be called by each QT application. The program run stops here waiting for events such as keyboard events or mouse events to occur, waiting for the user to manipulate the window.
And what do we mean by the code we write?
Qwidget W; qwidget is the QT window base class, which creates an object W according to Qwidget, which means that W is a Window object.
w.show (); Window creation is hidden by default and needs to be called show () to display it
The appearance of a QT program is usually made up of objects of various classes.
parent window Add Child parts
Next, we add a button to the original blank window, which is implemented by specifying the parent object, with the following flow:
1) Create the desired button
2) button Specifies that the window is the parent object, that is, the button is placed on the window
3) Display button
The modified code is as follows:
#include <QApplication> #include <QWidget> #include <QPushButton>//button required header file int main (int argc, char * Argv[] {qapplication app (argc, argv);//Initialize Qwidget w;w.show ();//new Code//Create a button with "^_^", Need header file: #include < Qpushbutton>qpushbutton button ("^_^"); The button specifies that the window is the parent object, i.e. the button is placed on the window button.setparent (&w); Button.show (); Display button app.exec (); Main Event loop return 0;}
Next, tap make to recompile the code and run the result as follows:
A part specifies that the B part is the parent object, and the direct effect we see is that the a part is placed above the B part, in fact, the specified parent object has the following effect:
1) When the parent object shows, it will recursively call all of its child objects, so that they are all displayed.
2) When the parent object is destroyed, it will recursively destroy all of its child objects, making the memory management more simple.
There are two main ways to specify a parent object: 1) call SetParent () function 2) when creating a new part by assigning to a constructor parameter.
The perfect code is as follows:
#include <QApplication> #include <QWidget> #include <QPushButton>//button required header file int main (int argc, char * Argv[] {qapplication app (argc, argv);//Initialize Qwidget w;//Create a button with content "^_^", Need header file: #include <qpushbutton>// The Qpushbutton button ("^_^", &w) is assigned to the parent object by specifying W for the constructor argument. W.show (); Displays the parent window, and the part on the parent window will also display app.exec (); Main Event loop return 0;}
The program works the same as above.
For this tutorial sample code download Please click on this link: Http://download.csdn.net/detail/tennysonsky.
Copyright NOTICE: This blog post, mostly I compiled, or in the network collection, reproduced please indicate the source!!
QT Introductory Learning--QT Quick Start (vim pure code writing)