QT Learning Pathway (3): hello,world! Continued

Source: Internet
Author: User
Tags wxwidgets

Here is a step-by-step explanation of the previous Hello, world! program, although very simple, but it can be a clear understanding of the structure of the QT program. Now post 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.exec();
}

Lines 1th and 2nd are header files that need to be introduced. As with ordinary C + + programs, if you want to use a component, you have to introduce the corresponding header file, which is similar to the Java import mechanism. It is worth noting that the header file and the class name in QT are consistent. In other words, if you want to use a class, its class name is its header file name.

Line 3rd is a blank line:)

Line 4th is the header of the main function. This is not the same as ordinary C + + programs, learn C + + understand. So you can see that, in fact, Qt comes in completely through the ordinary main function, which is different from wxwidgets, because Wxwidgets's Hello, World needs you to inherit one of its Wxapp classes and overwrite its Wxapp::oninit method, The system automatically compiles the OnInit into a portal function. But in Qt, there's no need for this.

Line 5th, oh oh, curly braces ...

Line 6th, create a Qapplication object. This object is used to manage resources at the application level. The Qapplication constructor requires two parameters, one from main, so that QT is to some extent supported by command-line arguments.

Line 7th, create a Qlabel object and be able to display the Hello, world! string. As with other library label controls, 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 the controls (controls) and containers (containers) inside Windows technology. In other words, widgets can place other widgets, just like swing's components. Most QT programs use Qmainwindow or Qdialog as top-level components, but QT does not enforce this. In this example, the top-level component is a qlabel.

Line 8th to make this label visible. Components are usually invisible after they are created and require that we manually make them visible. In this way, we can customize them after we create the build to avoid flashing on the screen after the appearance.

Line 9th, transfer control of the application to QT. At this point, the program's event loop begins, that is, you can then correspond to the various events you send. This is similar to the last line of GTK + Gtk_main ().

Line 10th, curly braces ... The program is over.

Note that we do not use Delete to delete the created Qlabel because the operating system reclaims the space after the program is finished-only because this qlabel takes up less memory, but sometimes it can cause trouble, especially in large programs, so be careful.

Well, the procedure is finished. According to the normal process, the following should be compiled. As mentioned earlier, QT compilation cannot use normal make, but must first be precompiled using Qmake. Therefore, the first step should be in the engineering directory to use

Qmake-project

command to create a. Pro file (for example, called Helloworld.pro). And then use it in the. Pro file directory.

Qmake Helloworld.pro (make)

Or

QMAKE-TP VC Helloworld.pro (NMAKE)

A makefile is generated before the call to make or NMAKE is compiled. But because we're using the IDE, these steps don't need to be done manually.

It is worth noting that this qmake is capable of generating standard makefile files, so it is entirely possible to automatically generate makefile--using Qmake this is a digression.

OK, the following changes the source code, the Qlabel to create a sentence to

Qlabel *label = new Qlabel ("

Run it:

Like Swing's JLabel, QT also supports HTML parsing.

Well, this Hello, world is here! To clarify QT's program structure, in a QT source code, the two statements are essential:

QApplication app(argc, argv);
//...
return app.exec();

Source: http://devbean.blog.51cto.com/448512/194137

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.