1. 程式人生 > >《MFC&Qt混合程式設計》 part1 MFC對話方塊中嵌入Qt視窗控制元件

《MFC&Qt混合程式設計》 part1 MFC對話方塊中嵌入Qt視窗控制元件

《MFC&Qt混合程式設計》 part1 MFC對話方塊中嵌入Qt視窗控制元件

1、環境配置:VS2010,Qt4.8.4_win64,Qt_VS_Addin_1.1.11

2、建立的MFC對話方塊程式

3、輸出為x64的debug&release程式

準備工作:

下載QtMigration檔案

第一步:

建立一個最簡單的 MFC Dialog 程式——編譯應該是正常的MFC程式。

第二步:.

1、將下載後的source檔案中qmfcapp qwinhost qwinwidget 六個對應的.h .cpp檔案新增到工程中

2、新增對應的Qt標頭檔案:

include

include “qmfcapp.h”

include “qwinhost.h”

include “qwinwidget.h”

3、在 BOOL CTestApp::InitInstance() 函式中增加一行程式碼:

QMfcApp::instance(this);

跟蹤到QMfcApp的程式碼中可以清楚的看到該靜態函式的功能是建立QApplication例項。

第三步:

重寫CTestApp的run方法——在標頭檔案中新增virtual int run();
cpp中的函式如下:

int CTestApp::Run()
{
int result = QMfcApp::run(this);
delete qApp;
return result;
}

注——QT幫助介紹:
QMfcApp:run()
will then use that QMfcApp::instance, which must then be deleted explicitly using the global qApp pointer.

第四步:

在testDlg.h檔案中新增定義——注意新增Qt的標頭檔案:

QWinWidget *widget;

第五步:

1、在工程中右擊——類嚮導——類名要對應到testDialog這個類——到左下面的訊息tab框——雙擊分別選中WM_CREATE和WM_DESTORY——確定

2、在CTestDialog的OnCreate函式中加入下面程式碼:

widget = new QWinWidget( this );
QHBoxLayout *hbox = new QHBoxLayout( widget );
QLabel *label = new QLabel( “Enter text:”, widget );
QLineEdit *edit = new QLineEdit( widget );
hbox->addWidget( label );
hbox->addWidget( edit );
widget->move( 0, 0 );
widget->show();

注:記得加上對應的標頭檔案或Qt宣告:

include “qwinwidget.h”

include

include

include

include