詳解QT源碼之QT創(chuàng)建窗口程序、消息循環(huán)和WinMain函數(shù)
QT源碼之QT創(chuàng)建窗口程序、消息循環(huán)和WinMain函數(shù)是本文要介紹對(duì)內(nèi)容,使用QT也有一段時(shí)間了,有的時(shí)候需要跟蹤代碼到QT的源碼中去查找問(wèn)題。在這里我將記錄一下我跟蹤QT源碼學(xué)習(xí)到的一些知識(shí)。
我的開(kāi)發(fā)環(huán)境是VC6.0+QT4.3.3。QT已經(jīng)不為VC6.0提供addin了,所以有的時(shí)候我也會(huì)使用EclipseCDT來(lái)編寫(xiě)代碼,因?yàn)橛辛薗T for Eclipse的plugin寫(xiě)代碼會(huì)方便一些。
我們?cè)趯W(xué)習(xí)QT的時(shí)候,接觸的***個(gè)程序就是下面的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();
- }
這個(gè)程序的作用很多手冊(cè)和文檔都已經(jīng)講了,講的也都很細(xì)致,非常不錯(cuò)。
但是喜歡鉆研,深入的童鞋也許開(kāi)始注意了int main(int argc, char *argv[]),這個(gè)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)的?
下面我們將來(lái)一起學(xué)習(xí)一下QT的源碼來(lái)解釋一下這個(gè)main函數(shù)和整個(gè)窗口程序的創(chuàng)建過(guò)程:
設(shè)置好路徑后,我們先F10一下,看看這個(gè)程序到底是從哪里開(kāi)始運(yùn)行的。
程序跳到了\winmain\qtmain_win.cpp文件的WinMain函數(shù)中,再看這個(gè)文件上面的宏定義:#define main qMain
繼續(xù)看:在WinMain函數(shù)中調(diào)用了我們自己定義的main函數(shù):int result = main(argc, argv.data());
哇塞,原來(lái)如此啊。原來(lái)我們寫(xiě)的main函數(shù)是假的。哈哈。
再來(lái)看一下QT是如何創(chuàng)建窗體和消息循環(huán)的。
首先我們來(lái)到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ù)。大家注意第三個(gè)參數(shù):QApplication::Type type
這事Type類(lèi)型的定義:enum Type { Tty, GuiClient, GuiServer };
下面是代碼注釋中對(duì)Type類(lèi)型的解釋?zhuān)?/p>
- \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)用了類(lèi)似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),原來(lái)窗口消息都是在這里進(jìn)行處理的啊!
至于***一句app.exec(); 調(diào)用了QCoreApplication的Exec函數(shù),在這個(gè)函數(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)非常清楚了。