From the C ++ to the Qt (IDE or discarded qmake, bound cmake tools such attempts by a few examples)

Qt is a C ++ library, Qt was a little extended on the basis of the ansi C ++.

But the country seems to be more impetuous, learning a lot of even basic Qt C ++ compiler how to seem less clear. This article abandon IDE or bound qmake, cmake and other tools, try a few examples, step by step transition from standard C ++ compiler to compile the Qt.

Are the most basic things in this article, perhaps it can be said, whether through which tool (qmake, cmake, boost.build, qtcreator, vs2008, Eclipse, ...), the contents of this article as long as you are using C ++ Qt It is to be understood (although actually write the program, we will not be directly used to compile Qt C ++ compiler program).

If you compare the fear of the command line, you may wish to look at my original finishing the  GCC Getting Started

Example 1: simple console program


A very simple example, it's useless to Qt Extended :( In other words, this is a normal C ++ program)

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
int main(int argc, char** argv) { QCoreApplication app(argc, argv); qDebug()<<"hello qt!"; app.exec(); } 

We all know that compiling a C ++ program, is nothing more than pre-compile, compile, link

  • Compiler preprocessor: header file path and the necessary macros
  • Compiler: Some compilers parameters
  • Linker: links parameters and to link library

g++

Simple command line, to generate main.exe (lower linux, generating executable main)

g++ main.cpp -DQT_CORE_LIB -Ie:\Qt\4.7.0\include -o main -Le:\Qt\4.7.0\lib -lQtCore4

Line command is very simple:

  • -I specifies the path to the header file
  • -L specifies the library file path
  • -l need to specify link libraries
  • -D necessary to define the macro (in fact, this little program, did not need to use this macro)
  • -o specifies that the resulting executable file name

cl

Simple command line, you can generate main.exe

cl main.cpp -ID:/Qt/4.7.0/include -DQT_CORE_LIB -Femain -link -LIBPATH:D:/Qt/4.7.0/lib QtCore4.lib

Still very simple

  • -I include path
  • -D necessary to define macros
  • -Fe specify the executable file name
  • -Link is behind the link parameters
    • -LIBPATH library file path

Example 2: simple GUI program


This time a little more complex, not a single console program, but a simple GUI program

  • main.cpp

    #include <QtGui/QApplication>
    #include "widget.h"
    
    int main(int argc, char** argv) { QApplication app(argc, argv); Widget w; w.show(); return app.exec(); } 
  • widget.h

    #include <QtGui/QWidget>
    class Widget : public QWidget { public: Widget(QWidget * parent=NULL); }; 
  • widget.cpp

    #include "widget.h"
    
    Widget::Widget(QWidget * parent) :QWidget(parent) { } 

Again, this program does not expand the use of Qt, the direct use of C ++ compiler:

g++

g++ main.cpp widget.cpp -DQT_CORE_LIB -DQT_GUI_LIB -Ie:\Qt\4.7.0-beta2\include -o main -Le:\Qt\4.7.0-beta2\lib -lQtCore4 -lQtGui4

Because we use QtGui module, so compared to the previous:

  • Increased -DQT_GUI_LIB and -lQtGui4
  • More than one file widget.cpp

Note: Under Windows

If the non-windows platforms, this command on it. But under the windows, you know: the sub-console and two windows to link the subsystems, and the main entry points and function WinMain.

This command, compile the main.exe console will pop up. To do console, use the following command:

g++ main.cpp widget.cpp -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NEEDS_QMAIN -Ie:\Qt\4.7.0-beta2\include -o main -Le:\Qt\4.7.0-beta2\lib -lQtCore4 -lQtGui4 -lqtmain -Wl,-subsystem,windows

Two more options:

  • qtmain the library a WinMain function, it calls the main function of our code. That is for the compiler: the name of the entry function has changed
  • -Wl, -subsystem, windows you know, links windows subsystem
  • And MinGW for it, here one more macro QT_NEEDS_QMAIN, this thing is very interesting. In Qt Windows link subsystem and entry point (the final version), we mentioned this in detail. (Here, but you can ignore it, no errors, and there will be no appreciable difference)

cl

With windows in g ++ is basically the same, with the console:

cl main.cpp widget.cpp -ID:/Qt/4.7.0/include -DQT_CORE_LIB -DQT_GUI_LIB -Femain -link -LIBPATH:D:/Qt/4.7.0/lib QtCore4.lib QtGui4.lib

Without console:

cl main.cpp widget.cpp -ID:/Qt/4.7.0/include -DQT_CORE_LIB -DQT_GUI_LIB -Femain /MD -link -LIBPATH:D:/Qt/4.7.0/lib -subsystem:windows qtmain.lib QtCore4.lib QtGui4.lib

Analysis above: specify the link subsystem, enabled WinMain entry function

How to manage multi-file program

The compiler directly call any harm it?

  • Parameters, ah, manually enter each time, fallible. (Example we use the parameters have been as little as possible, and may make you feel dizzy or have a).
  • Secondly it, a very important point, as long as every time a file is modified, all things will be recompiled.

The way to change this situation, the traditional is to write Makefile, then need to enter only make compile time on the line, he will determine which files need to be recompiled been altered.

The other is the VS IDE and some provide their own function. Following is a brief look at this example correspond to the makefile:

mingw32-make of the Makefile

CPPFLAGS = -DQT_CORE_LIB -DQT_GUI_LIB -Ie:\Qt\4.7.0\include
LDFLAGS = -Le:\Qt\4.7.0\lib -lQtCore4 -lQtGui4 -lqtmain -Wl,-subsystem,windows objects = main.o widget.o dest = main $(dest) : $(objects) g++ -o $@ $(objects) $(LDFLAGS) 

Makefile for nmake

CPPFLAGS = -ID:/Qt/4.7.0/include -DQT_CORE_LIB -DQT_GUI_LIB -MD
LDFLAGS = -LIBPATH:D:/Qt/4.7.0/lib -subsystem:windows qtmain.lib QtCore4.lib QtGui4.lib
objects = main.obj widget.obj dest = main.exe $(dest) : $(objects) link $(objects) $(LDFLAGS) 

Which is not introduced, because writing is a science Makefile. Quite difficult to write, all have qmake, cmake these tools to help us generate the Makefile

Example 3: introduction moc


Qt extension to the C ++ are mainly three aspects:

  • Element object system files (.h, .cpp etc.) comprising Q_OBJECT macro requires pretreatment moc
  • Resources system, .qrc files need to be pre-rcc
  • Interface system, .ui documents preprocessing uic

These three are among the most complex meta-object system, Qt program is important. The other two you can not, except that not a bit outrageous (also called Qt program did what? As we wrote earlier, just an ordinary C ++ program)

BS, see the foregoing modified example :( widget.h, was added the Q_OBJECT)

#include <QtGui/QWidget>
class Widget : public QWidget { Q_OBJECT public: Widget(QWidget * parent=NULL); }; 

How to compile this program do? Examples II commands still work? Try:

Wow, the output of good rich ah!

Greetings from the g ++:

main.o:main.cpp:(.text$_ZN6WidgetD1Ev[Widget::~Widget()]+0xb): undefined reference to `vtable for Widget'
main.o:main.cpp:(.text$_ZN6WidgetD1Ev[Widget::~Widget()]+0x15): undefined reference to `vtable for Widget' widget.o:widget.cpp:(.text+0x39): undefined reference to `vtable for Widget' widget.o:widget.cpp:(.text+0x43): undefined reference to `vtable for Widget' 

Greetings from cl of:

widget.obj: error LNK2001: unresolved external symbol "public: Virtual QMetaObject const struct * __thiscall Widget :: MetaObject (void) const" (MetaObject Widget @@ @ @@ UBEPBUQMetaObject the XZ?) 
widget.obj: error LNK2001: can not unresolved external symbol "public: void * Virtual __thiscall Widget :: qt_metacast (const char *)" (qt_metacast Widget @@ @ @ the Z-UAEPAXPBD?) 
widget.obj: error LNK2001: unresolved external symbol "public: virtual int __thiscall Widget :: qt_metacall what happened (enum QMetaObject :: Call, int, void * *) "(? qt_metacall @ Widget @@ UAEHW4Call @ QMetaObject @@ HPAPAX @ Z)?

After adding a macro, what happened? We look after the compiler macro expansion will look like:

#include <QtGui/QWidget>
class Widget : public QWidget { static const QMetaObject staticMetaObject; virtual const QMetaObject *metaObject() const; virtual void *qt_metacast(const char *); virtual int qt_metacall(QMetaObject::Call, int, void **); ... public: Widget(QWidget * parent=NULL); }; 

All of a sudden so many extra functions, but also no function body, not strange error. How to generate the function body do? This is done moc:

power widget.h -o moc_widget.cpp

As a result, these functions are implemented in moc_widget.cpp, as long as we compile the document a link on the line

For g ++ is, on the basis of two examples, direct moc_widget.cpp add a file, and then everything is normal:

g++ main.cpp widget.cpp moc_widget.cpp -DQT_CORE_LIB -DQT_GUI_LIB -Ie:\Qt\4.7.0-beta2\include -o main -Le:\Qt\4.7.0-beta2\lib -lQtCore4 -lQtGui4

To cl compiler, the same as long as you can add a moc_widget.cpp.

Examples of four, rcc and uic


A little spoil the name of this section do not speak example (because the concept is relatively simple uic and rcc)

  • If we use the resources, you need a xxx.qrc file, the file does, C ++ compiler does not know, so

    rcc xxx.qrc -o qrc_xxx.cpp
    
  • If we use the designer interface designed .ui. C ++ compiler does not know this document, so

    uic xxx.ui -o ui_xxx.h
    

As a result, we get to be on all .h and .cpp files of the remaining work, you know, to the C ++ compiler on the line.

other


Now look at this chart: Is not it simple?

From C++ to Qt

2015 Update


For Qt5 user, this basic application, just pay attention

  • In Qt4 gui in Qt5 mainly divided into Qt5Gui, Qt5Widget and several libraries.
Qt4 Qt5
QtCore4 Qt5Core
QtGui4 Qt5Gui Qt5Widget
  • Header QtGui / QWidget other needs become QtWidgets / QWidget

http://blog.debao.me/zh/2010/11/from-cpp-to-qt/

Guess you like

Origin www.cnblogs.com/findumars/p/11515527.html