Qt learning path (3): Hello, world! (Continued)

Source: Internet
Author: User
Tags wxwidgets
Next we will explain the preceding Hello, world! line by line! Program, although very simple, but can have a clear understanding of the structure of the QT program. Now paste the code: # include <qapplication>
# Include <qlabel>

Int main (INT argc, char * argv [])
{
Qapplication app (argc, argv );
Qlabel * label = new qlabel ("Hello, world! ");
Label-> show ();
Return app.exe C ();
} Lines 1st and 2nd are header files to be introduced. Similar to a common C ++ Program, to use a component, you must introduce the corresponding header file, which is similar to the Java import mechanism. It is worth noting that the header file in QT is consistent with the class name. That is to say, if you want to use a class, its class name is its header file name. The first row is a blank row. The second row is the main function header. This is no different from the common C ++ program. I have learned C ++ and understand it. Therefore, you can see that, in fact, QT enters completely through the normal main function, which is different from wxWidgets, because wxWidgets Hello, world requires you to inherit its wxapp class, and overwrite its wxapp: oninit method. The system automatically compiles oninit into an entry function. However, in QT, This is not required. Row 3, oh, braces... Row 3 creates a qapplication object. This object is used to manage application-level resources. The constructor of qapplication requires two parameters from the two main parameters. Therefore, QT supports command line parameters to a certain extent. Row 3 creates a qlabel object and displays Hello, world! String. Like the label control of other libraries, this is used to display text. In QT, this is called a widget (translation is a small thing, but this translation is not good ...), It is equivalent to controls and containers in Windows technology ). That is to say, widgets can be placed with other widgets, just like swing widgets. Most QT programs use qmainwindow or qdialog as top-level components, but QT does not require this. In this example, the top-level component is a qlabel. 8th rows to make the label visible. Components are usually invisible after they are created. We need to manually make them visible. In this way, we can customize the components after they are created to avoid flickering on the screen after they appear. Line 3: hand over control of the application to QT. At this time, the program's event loop begins, that is, you can now send various events accordingly. This is similar to the last line of gtk_main () in GTK + (). 10th rows, braces ...... The program is finished. Note: We didn't use Delete to delete the created qlabel, because the operating system will reclaim the space after the program ends. This is only because the qlabel occupies a small amount of memory, but sometimes this will cause trouble, especially in large programs, so you must be careful. All right, the program has been explained. Follow the normal process and compile the code below. As mentioned above, QT compilation cannot use normal make, but must be precompiled with qmake. Therefore, the first step is to use the qmake-Project command in the project directory to create a. Pro file (for example, helloworld. Pro ). Then, use qmake helloworld. Pro (make) or qmake-tp vc helloworld. Pro (nmake) in the. Pro file directory to generate the makefile, and then call make or nmake for compilation. However, because we use IDE, we do not need to manually complete these steps. It is worth noting that this qmake can generate standard makefile files, so it is completely possible to use qmake to automatically generate makefile-this is a digress. Now, modify the source code and change the qlabel creation statement to qlabel * label = new qlabel ("<H2> <font color = 'red'> Hello </font>, world! <H2> "); run: Like swing's jlabel, QT supports HTML parsing. Okay, this Hello, world is here! Clarify the program structure of QT. In a QT source code, the following two statements are essential: qapplication app (argc, argv );
//...
Return app.exe C ();

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.