您的位置:首页 > 编程语言 > Qt开发

《MFC&Qt混合编程》 part1 MFC对话框中嵌入Qt窗口控件

2018-03-07 16:57 1071 查看
看到一篇写的靠谱的MFC与Qt混合编程的文章,mark一下,转自

http://blog.csdn.net/sinat_24206709/article/details/77185974

《MFC&Qt混合编程》 part1 MFC对话框中嵌入Qt窗口控件

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

2、创建的MFC对话框程序

3、输出为x64的debug&release程序

准备工作:

下载QtMigration文件

http://download.csdn.net/download/power_YQ/3539936

第一步:

创建一个最简单的 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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: