Qt entry, the simplest QT Program

Source: Internet
Author: User
Tags net command net command line

I need to study qt recently because of my work. Objectively speaking, as a cross-platform code framework, qt is still good. I won't repeat it if there are a lot of articles on the Internet that boast qt. However, qt is really not cute, probably because there are too few people. qt books, sample code, help systems, auxiliary tools, community support, and so on are far from enough. Of course, I am not so familiar with Microsoft. If I get used to Microsoft, I should take Microsoft as the standard, how many people are there in Microsoft (in fact, it is even worse how many people are there by Microsoft), it is really difficult for small companies to compare with this giant. Too much nonsense. Anyway, I think qt is quite uncomfortable in many places. If it is redundant, you will not need to talk about it.

To learn about qt, first install the compiling environment of qt. Here I use qt4 (qt4 has very few materials in this year). It is developed in a windows environment, compile the tool with Visual. net 2005 (it is estimated that a large film will faint, it is not good, 2002 and so on), the installation process is relatively simple, it is omitted here. The final standard is that you can run the built-in example of qt. In fact, these examples are pretty, and they are cross-platform.

Write the first qt program and create a directory, such as d:/qt/study/hello, and then create a main program. cpp empty file, start to write code, in fact, the simplest qt program only a few words:

# Include <QApplication> // contains the QApplication header file
# Include <QPushButton> // contains the QPushButton header file

Int main (int argc, char * argv []) // It is also a standard main function.
{
QApplication app (argc, argv); // construct an application, same as WinApp in MFC
QPushButton hello ("Hello world! "); // Construct PushButton
Hello. resize (100, 30); // set the button size
Hello. show (); // display button

Return app.exe c (); // execute the application, same as Run in MFC
}

The comments are well-written. Just look at them. Finally, we compile the program. qt compilation is not very friendly. Simply put, it is:

Open Visual Studio. Net command line
Go to the d:/qt/study/hello directory (this directory is just an example. You should know to enter your own directory)
Run qmake-project to generate the qt project file, which ends with the pro extension.
Run qmake-t vcapp to generate a VC project that can be opened with. Net 2005.
Execute qmake to generate the Makefile file, instead of letting me hand-Write the file, I am already thankful :)
Run nmake to compile the Makefile file.

Now, find hello.exe in the debug‑releasedirectory and execute it. A qt program with only one button is born.

Of course, if we only consider the number of lines of code, the simplest qt program should be the following console program.

# Include <QApplication> // contains the QApplication header file

// Be sure to modify the linker/System in the Project Properties and set it as a console program.
Int main (int argc, char * argv []) // It is also a standard main function.
{
QApplication app (argc, argv, QApplication: Tty); // construct an application, same as WinApp in MFC
Printf ("Hello world! /N ");
Getchar ();
Return 0;
}

However, beginners may prefer the colorful interfaces.
Our Hello World is complete. To tell the truth, it's cool, but I am a lazy person. So the troublesome compilation process should be simplified a little, so I wrote a qt. bat's batch processing program, which contains only four sentences

Qmake-project
Qmake-t vcapp
Qmake
Nmake

In this way, the compilation process is simplified:

Open Visual Studio. Net command line
Go to the d:/qt/study/hello directory (this directory is just an example. You should know to enter your own directory)
Run qt to execute commands in qt. bat.

Although I only gave birth to three steps, I still felt troublesome, mainly because it was annoying to locate the directory. Although the command line supports the mouse-drag positioning function, I still have to knock a cd, in addition, you must use Visual Studio.. Net command line, rather than common cmd

Look at the properties of the Visual Studio. Net command line program. It is nothing more than the following command

% Comspec %/k "D:/Program Files/Microsoft Visual Studio. NET/Common7/Tools/vsvars32.bat"

Then you can directly write an MFC program to handle these issues. The core code is only the following lines:

Void CTestDlg: OnButton1 ()
{
// TODO: Add your control notification handler code here
UpdateData ();
CString strDir;
M_Dir.GetWindowText (strDir );
If (! StrDir. IsEmpty ())
{
SetCurrentDirectory (strDir );
System ("% comspec %/K/" D: // Program Files // Microsoft Visual Studio. NET // Common7 // Tools // qtex. bat /"");
}
}

Void CTestDlg: OnDropFiles (HDROP hDropInfo)
{
// TODO: Add your message handler code here and/or call default
Char szFileName [512];
Int nfilecount =: dragqueryfile (hdropinfo, 0 xffffffff, null, 512 );
For (INT I = 0; I <nfilecount; I ++)
{
Uint nchars =: dragqueryfile (hdropinfo, I, szfilename, 512 );
Cstring STR (szfilename, nchars );
M_dir.setwindowtext (STR );
}
: Dragfinish (hdropinfo );
 
Cdialog: ondropfiles (hdropinfo );
}

Have you seen my purpose? I made a simple dialog box Program, implemented the positioning of the QT directory by dragging, and then clicked the compile button to compile the QT program, in fact, even the compile button can be omitted simply. If you drag it directly, you can compile it and perform one step operation. This is more suitable for my character :) As for the qtex. bat, in fact, just copied vsvars32.bat, changed the name, and added QT. bat content (of course, you can add only one line of QT and directly call QT. bat also achieves reuse)

This article is really simple. It is difficult to start with everything. The key is interest. It is best to see something. In this sense, hello World! Almost the first example of language learning is not unreasonable.

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.