詳解QT源碼之QT創(chuàng)建窗口程序、消息循環(huán)和WinMain函數(shù)
QT源碼之QT創(chuàng)建窗口程序、消息循環(huán)和WinMain函數(shù)是本文要介紹對內(nèi)容,使用QT也有一段時(shí)間了,有的時(shí)候需要跟蹤代碼到QT的源碼中去查找問題。在這里我將記錄一下我跟蹤QT源碼學(xué)習(xí)到的一些知識。
我的開發(fā)環(huán)境是VC6.0+QT4.3.3。QT已經(jīng)不為VC6.0提供addin了,所以有的時(shí)候我也會使用EclipseCDT來編寫代碼,因?yàn)橛辛薗T for Eclipse的plugin寫代碼會方便一些。
我們在學(xué)習(xí)QT的時(shí)候,接觸的***個程序就是下面的helloworld程序:
- view plaincopy to clipboardprint?
- #include <QApplication>
- #include <QPushButton>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QPushButton hello("Hello world!");
- hello.resize(100, 30);
- hello.show();
- return app.exec();
- }
- #include <QApplication>
- #include <QPushButton>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QPushButton hello("Hello world!");
- hello.resize(100, 30);
- hello.show();
- return app.exec();
- }
這個程序的作用很多手冊和文檔都已經(jīng)講了,講的也都很細(xì)致,非常不錯。
但是喜歡鉆研,深入的童鞋也許開始注意了int main(int argc, char *argv[]),這個main函數(shù)是標(biāo)準(zhǔn)的main函數(shù),而windows應(yīng)用程序的入口是winmain函數(shù),而main函數(shù)是命令行程序的入口。win下窗口程序都有RegisterClass,和消息循環(huán),QT是如何RegisterClass和創(chuàng)建消息循環(huán)的?
下面我們將來一起學(xué)習(xí)一下QT的源碼來解釋一下這個main函數(shù)和整個窗口程序的創(chuàng)建過程:
設(shè)置好路徑后,我們先F10一下,看看這個程序到底是從哪里開始運(yùn)行的。
程序跳到了\winmain\qtmain_win.cpp文件的WinMain函數(shù)中,再看這個文件上面的宏定義:#define main qMain
繼續(xù)看:在WinMain函數(shù)中調(diào)用了我們自己定義的main函數(shù):int result = main(argc, argv.data());
哇塞,原來如此啊。原來我們寫的main函數(shù)是假的。哈哈。
再來看一下QT是如何創(chuàng)建窗體和消息循環(huán)的。
首先我們來到QApplication的構(gòu)造函數(shù):
- QApplication::QApplication(int &argc, char **argv, int _internal)
- : QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient))
- { Q_D(QApplication); d->construct(); QApplicationPrivate::app_compile_version = _internal;}
很明顯,首先調(diào)用的是QApplicationPrivate的構(gòu)造函數(shù)。大家注意第三個參數(shù):QApplication::Type type
這事Type類型的定義:enum Type { Tty, GuiClient, GuiServer };
下面是代碼注釋中對Type類型的解釋:
- \enum QApplication::Type
- \value Tty a console application
- \value GuiClient a GUI client application
- \value GuiServer a GUI server application (for Qt for Embedded Linux)
當(dāng)程序運(yùn)行到hello.show()的時(shí)候調(diào)用了QWidgetPrivate::create_sys函數(shù)。
在這里我們看到調(diào)用了類似RegisterClass的函數(shù):QString windowClassName = qt_reg_winclass(q);
這里的q是指向QWidget的指針(我們先忽略掉這里)。
以及包括后面的CreateWindow,ShowWindow等等我們熟悉的WindowsAPI函數(shù)
const QString qt_reg_winclass(QWidget *w) 函數(shù)的原型是在qapplication_win.cpp中定義的。我們轉(zhuǎn)到qt_reg_winclass函數(shù)的實(shí)現(xiàn)中。我們就看到了windows的API函數(shù)RegisterClass和窗口消息處理函數(shù):wc.lpfnWndProc = (WNDPROC)QtWndProc;
我們看一下QtWndProc的實(shí)現(xiàn),原來窗口消息都是在這里進(jìn)行處理的啊!
至于***一句app.exec(); 調(diào)用了QCoreApplication的Exec函數(shù),在這個函數(shù)中我們看到了下面創(chuàng)建消息循環(huán)的代碼
- QEventLoop eventLoop;
- self->d_func()->in_exec = true;
- int returnCode = eventLoop.exec();
在QCoreApplication.cpp中的注釋是這樣解釋的:
- The application will enter
- the event loop when exec() is called. exit() will not return
- until the event loop exits, e.g., when quit() is called.
到這里,main和WinMain函數(shù)到底是怎么回事,以及QT是怎么創(chuàng)建窗口和消息循環(huán)的,我們已經(jīng)非常清楚了。