1. Compile through command line
1). Set Environment Variables
Path = D: \ QT \ 4.1.1 \ bin
Qmakespec = win32-msvc
Open a command window (if it has been opened before setting the environment variable, You need to close it and re-open it because the environment variable does not work) and check whether the settings are correct:
C: \> qmake-V
Qmake version 2.01a
Using QT version 4.3.2 In D: \ QT \ 4.3.2 \ Lib
C: \> echo % qmakespec %
Win32-msvc
2) create a directory where the source code is to be placed. Create a file named hello. cpp under the Directory and enter the following 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 ();
}
3) Compile the code
Under this directory, type "qmake-project-O hello. Pro" to generate the QT project file.
Then, type "qmake" to generate the MAKEFILE file.
Type "nmake" to compile the MAKEFILE file.
The compiled executable file is under the debug or release directory.
2.use designer.exe to design the interface
1). Create a dialog box using designer, save it in the project directory, and name it Hello. UI.
2) create a hello. h file with the code: # Include <qapplication>
# Include <qdialog>
# Include "ui_hello.h"
Class helloform: Public qdialog, public UI: Dialog
{
Q_object
Public:
Helloform (qwidget * parent = 0 );
};
3) Create the hello. cpp file with the code:
# Include "Hello. H"
Helloform: helloform (qwidget * parent)
: Qdialog (parent)
{
Setupui (this );
}
Int main (INT argc, char ** argv)
{
Qapplication app (argc, argv );
Helloform * form = new helloform ();
Form-> show ();
Return app.exe C ();
}
4) Compile and run the program in the following sequence:
Qmake-project-O hello. Pro
Qmake
Nmake
./Debug/hello.exe
Note: C ++ class declarations containing the QT interface cannot be completed in the. cpp file; otherwise, the connection will fail (MOC does not seem to be able to properly process the class declarations in the CPP file ).
There is another way to load the UI:
The content of Hello. H is as follows:
# Include <qapplication>
# Include <qdialog>
# Include "ui_hello.h"
Class helloform: Public qdialog //, public UI: Dialog
{
Q_object
Public:
Helloform (qwidget * parent = 0 );
PRIVATE:
Ui: Dialog UI;
};
The content of Hello. cpp is as follows:
# Include "Hello. H"
Helloform: helloform (qwidget * parent)
: Qdialog (parent)
{
Ui. setupui (this );
// Setupui (this );
}
Int main (INT argc, char ** argv ){
Qapplication app (argc, argv );
Helloform * form = new helloform ();
Form-> show ();
Return app.exe C ();
}
Compile and run in the same way as above.
3. Use VC ide to write programs
Run the command "qmake-tp vc-O hello. DSP hello. pro ". At this time, the" hello. DSP.
Use the VC ide To compile and run the program.