自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解QT源碼之QT創(chuàng)建窗口程序、消息循環(huán)和WinMain函數(shù)

移動開發(fā)
本文介紹的是QT源碼之QT創(chuàng)建窗口程序、消息循環(huán)和WinMain函數(shù)的內(nèi)容,先來看內(nèi)容!

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程序:

  1. view plaincopy to clipboardprint?  
  2. #include <QApplication>     
  3. #include <QPushButton>     
  4.     
  5. int main(int argc, char *argv[])     
  6. {     
  7.     QApplication app(argc, argv);        
  8.     QPushButton hello("Hello world!");     
  9.     hello.resize(100, 30);       
  10.     hello.show();     
  11.     return app.exec();     
  12. }    
  13. #include <QApplication> 
  14. #include <QPushButton> 
  15.  
  16. int main(int argc, char *argv[])  
  17. {  
  18.     QApplication app(argc, argv);   
  19.     QPushButton hello("Hello world!");  
  20.     hello.resize(100, 30);   
  21.     hello.show();  
  22.     return app.exec();  
  23. }  

這個程序的作用很多手冊和文檔都已經(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ù):

  1. QApplication::QApplication(int &argc, char **argv, int _internal)  
  2.     : QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient))  
  3. { 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類型的解釋:

  1. \enum QApplication::Type  
  2.  
  3. \value Tty a console application  
  4. \value GuiClient a GUI client application  
  5. \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)的代碼

  1. QEventLoop eventLoop;  
  2. self->d_func()->in_exec = true;  
  3. int returnCode = eventLoop.exec(); 

在QCoreApplication.cpp中的注釋是這樣解釋的:

  1. The application will enter  
  2. the event loop when exec() is called. exit() will not return  
  3. until the event loop exits, e.g., when quit() is called. 

到這里,main和WinMain函數(shù)到底是怎么回事,以及QT是怎么創(chuàng)建窗口消息循環(huán)的,我們已經(jīng)非常清楚了。

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-06-23 15:32:05

Qt Windows消息

2011-06-23 15:10:39

Qt 窗體

2011-06-23 14:05:32

Qt 事件機(jī)制

2011-06-23 13:38:27

QT 元對象 信號

2011-06-23 11:16:39

Qt Excel

2011-06-28 16:18:24

Qt QObject

2011-07-01 13:03:32

QT 線程 串口

2011-06-28 13:12:07

Qt 調(diào)用 DLL DLL

2011-04-02 09:17:38

2011-06-23 14:27:48

QT QLibrary 動態(tài)庫

2011-06-10 11:05:05

Qt Quick QML

2011-06-30 09:46:01

QT 顯示視頻 linux

2011-06-23 14:40:13

Qt 信號

2011-06-24 17:38:09

Qt 坐標(biāo) 事件

2011-06-28 13:38:15

Arm linux QT

2011-06-24 10:05:51

QT 對象 父對象

2011-06-30 10:50:39

Qt OpenCV

2011-06-15 14:38:01

QT 信號

2011-07-05 18:51:51

QT 控件 鼠標(biāo)

2011-06-29 14:23:08

Qt 串口
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號