Qt4 Hello world example

Source: Internet
Author: User
Integrate qt4 with vs2005

After installing the qt4 development environment, I have never carefully studied it. I have read the official demo, but as a newbie, it is better to do it step by step!

 

So hello World!

 

Create a QT application project in vs2005 named "qt_test1"

 

This project has a main. cpp file, our main file.

 

# Include "qt_test1.h" // This can also be removed
# Include <qtgui/qapplication>
# Include <qlabel>

Int main (INT argc, char * argv [])
{
Qapplication app (argc, argv );

// Qt_test1 W; // added by default, commented out
// W. Show (); // added by default by the system, commented out

Qlabel * label = new qlabel ("Hello world! ");
Label-> show ();

Return app.exe C ();
}

 

Note:

1. Include qapplication is necessary. It is okay when you write <qapplication> or <qapplication. h>.

 

2. Because we use qlabel as the carrier of the text, we must also include qlabel. Of course, if you use any microware, You Can inclue which microware!

 

3.Qapplication app (argc, argv );

Return app.exe C ();

These two lines of code are necessary. The first line is to create a qapplication object to manage application resources. The second line is to let the program enter the message loop, wait for the possible menu, toolbar, mouse input to respond. (this is written in <C ++ GUI promgramming with qt4>. I just referenced it ).

 

4. qlabel * label = new qlabel ("Hello world! "); Dynamically creates a qlabel microfile with text.

Label-> show (); call the show method to display the image on the interface. Because the image is hidden by default during creation, it is necessary to explicitly display the image.

Qlabel supports HTML text format, such as "

Qt treats all micro-pieces such as qlabel and qpushbutton as application windows, not limited to qmainwindow or qdialog. Therefore, you can add other micro-pieces such as qmenubar to these micro-pieces, qtoolbar. the simplest verification method: New Two qlabel objects and display them !. Is there two windows?

It seems that all classes inherited from qwidget can be used as application windows.

 

5. finally, do not call delete at the end of the main method. I believe many c ++ experts know new and delete, and know that they will appear in pairs. Otherwise, the memory will be leaked. but don't worry, the memory will be recycled by the system after the application is terminated (dizzy, it depends entirely on the system ).

 

Press F5 to start debugging. A small window is displayed --!

 

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.