Qt--2, Hello World

Source: Internet
Author: User

1, write HelloWorld program

What is a HelloWorld program?

is to have the application display the "Hello World" string. This is the simplest application, but it contains the basic elements of an application, so it is generally used to demonstrate the process of creating a program. In this section you will be talking about creating a graphical user interface project in QT Creator to generate a program that can display the "Hello World" string.

Description of the files in the project directory:

File Description
Helloworld.pro The file is a project file that contains information about the project
Helloworld.pro.user This file contains information about the project for the user
Helloworld.h The file is the header file of the newly created Hellodialog class
Helloworld.cpp The file is the source file for the new Hellodialog class
Main.cpp The file contains the main () principal function
Hellodialog.ui This file is the interface file that the designer designed interface



Program run:

You can use the shortcut key Ctrl+r or run the program by pressing the Run button in the lower left corner.


Generated files:

Now the files in the project directory can be found, many: the Build-helloworld-desktop_qt_5_5_1_msvc2013_64bit-debug folder, which is the default build directory.

QT Creator classifies the project source files and the compiled files.

The HelloWorld folder is the project source file. The Build-helloworld-desktop_qt_5_5_1_msvc2013_64bit-debug folder, however, holds the files that are generated after compilation. Go to this folder to see there are 3 makefile files and a ui_helloworld.h file, there are two directories debug and release. Such as:


Now that the release folder is empty, go to the Debug folder, there are 3. obj files and a. cpp file, which are intermediate files generated at compile time and can be left out of the control, while the rest of the Helloworld.exe file is the resulting executable file.


Release of the program:

Now that the program has been compiled, how do you publish it so that it can run on someone else's computer?

First, compile the release version of the HelloWorld program in Qt Creator. Set the build target to release in the destination selector (target selector) in the lower-left corner. Such as:


After the compilation is complete, the new build in the project directory: Build-helloworld-desktop_qt_5_5_1_msvc2013_64bit-release folder, into the Release subfolder directory, The Helloworld.exe file has been generated.

Create a new folder, rename it to "My first Qt program", then copy the Helloworld.exe in the Release folder and go to the bin directory of the Qt installation directory (for example: C:\Qt\4.87.52\bin) to Mingwm10.dll, The 4 files of Libgcc_s_dw2-1.dll, QtCore4.dll and QtGui4.dll are copied over.

Now the whole folder has a total of 12MB, if the use of WinRAR and other packaging compression software to compress it, only 4MB, has reached an acceptable level, then you can release the compressed package.

Tips:

If a picture is used in a program other than PNG, the Imageformats folder in the plugins directory in the QT installation directory is copied to the Publisher folder when the program is published, as long as you keep the DLL file in the file format you use.

For example, if you use a GIF file, you only need to keep qgif4.dll.

If other modules are used in the program, such as Phonon, then the Phonon_backend folder in the plugins directory will be copied over.

Static compilation:

Static compilation is relative to the dynamic compilation mentioned earlier. Because, as seen earlier, in Qt creator by default, compiled programs need to include DLL files for publishing, which is called Dynamic compilation.

The static compilation is the QT library to recompile, with a statically compiled QT library to link the program, so that the resulting target file can be run directly, and no longer need to support the DLL file. However, the resulting EXE file is also very large, there are several MB, and static compilation lacks flexibility, and can not deploy plug-ins.

As you can see from the previous introduction, it is not very complicated to publish a program with several DLL files, and if you want to publish multiple applications at the same time, you can also share DLL files, so you can use the default method.

To learn more about QT release and static compilation methods, you can view the deploying QT applications keyword in the QT Creator Help index, and the Windows platform Publisher keyword is deploying an Application on Windows.


Reference header file, header file contains. In QT each class has a header file with the same name, because it uses the 3 classes of qapplication, Qdialog, and Qlabel, so here are the definitions of these classes include "Mainwindow.h" #include < qapplication> #include &LT;QLABEL&GT;/*QT the header file and the class name are consistent. That is, if you want to use a class, its class name is its header file name */int main (int argc, char *argv[])//main () function, which has two parameters to receive command-line arguments.                               {Qapplication A (argc, argv);//[in QT programs, the statement is essential]//Create a Qapplication object that is used to manage application-level resources The constructor of the qapplication requires two arguments, each from the two arguments//number from Main, so that QT is to some extent a support for command-line arguments    Number of. Qlabel *label = new Qlabel ("Hello world!"); /Create a Qlabel object and be able to display the Hello World//String. As with other libraries ' label controls, this is the label->show () that is used to display the text ();//The components are often invisible after they are created and require that we manually make them visible.    By default, new visual part objects are not visible, and you use the show () function to show them. return a.exec ();//return control of the application to QT. [In QT programs, this statement is essential]. Let the Qapplication object go into the event loop, so that when the QT application is running, it can receive the resulting events, such as clicking and Keyboard}//we didn't use Delete to delete the created Qlabel. Because the operating system will return//collect this space after the end of the program-it is only because the Qlabel occupies a small amount of memory, but sometimes doing so can lead//trouble, especially in large programs, so be careful
Output:


#include <QApplication> #include <QDialog> #include <QLabel> #include <QTextCodec>             // The Qtextcodec class provides a conversion function for text encoding int main (int argc, char *argv[]) {    qapplication A (argc, argv);    QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("UTF-8"));//Use the static function Setcodecfortr () in the Qtextcodec class to set the Qobject :: TR () Letter                                                                  //number the character set to use    Qdialog W;    Qlabel label (&W);    Label.settext (Qobject::tr ("Hello world! Hello, qt!. ");//To be able to display Chinese, you need to set the charset and then use the QOBJECT::TR () function to encode the string into    w.show ();    return a.exec ();}
                    The best place to put the SETCODECFORTR () function is in a program like this, placed underneath the Qapplication object in the main () function. All strings to be displayed to the interface////In the QT program                    are preferably enclosed in the TR () function, and for a string that is not to be displayed on the interface, if it contains Chinese, it can be encoded using qstring ().                    This needs to be set in the main function by adding the following code:                    //              qtextcodec::setcodecforcstrings (Qtextcodec::codecforname ("UTF-8"));


Qmake is a compilation tool provided by QT that can generate platform-independent. Pro files and then use the file to generate platform-related makefile files. The makefile file contains information such as the target file or executable file to be created, the files on which the target file is created, and the commands to run when each target file is created. Finally, using the make command to complete the automatic compilation, make is to read into the contents of the makefile file to perform the compilation work. When you use the Make command, a corresponding. o target file is generated for each source file, and finally the target files are linked to generate the final executable file


#------------ -------------------------------------# # Project created by Qtcreator 2013-08-07t23:10:24##------------------------- ------------------------QT + + core Guigreaterthan (Qt_major_version, 4): qt + = Widgetstarget = Helloworldtemplate = A Ppsources + = main.cpp Hellodialog.cppheaders + hellodialog.hforms + = Hellodialog.uirc_file + myico.rc 
Line 1th to 5th: Comment Information that describes when the file was generated. Line 6th: Indicates the module used by this project. The core module contains the central functionality of the QT non-graphical user interface, and all other modules are dependent on this module, while the GUI module expands the graphics interface         //polygon capabilities of the core module. That is, if you do not need to design a graphical interface of the program, then only the core module is required, but if the graphical interface, then it must include a GUI module. In fact           //The so-called module is a collection of many related classes, such as all the graphical interface related classes are in the GUI module, the reader can view the QT Help in the Qtcore module and Qtgui module off          //key word. Line 7th: is to add a line of code compatible with QT5, indicating that if it is a Qt5 version, add qt + = Widgets Line of code. Line 8th: Is the name of the generated target file, is the name of the generated EXE file, the default is the name of the project, of course, can be changed to another name here. Line 9th: Use the app template to indicate that this is an application. Lines 10th, 12 and 13: The source files, header files, and interface files included in the project, respectively. Line 14th: Is the file that adds the application icon. These files all use relative paths, because they are all in the project directory, so only the file names are written.




Qt--2, Hello World

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.