MFC与Qt混合编程

作者:程序开发之家
http://www.cngeng.info/qt%B1%E0%B3%CC/2044.html

现在,Windows下的大多数程序都是使用mfc开发的,由于众多产品想移植到非windows的操作系统上继续使用,qt由于自身“一次编写,到处编译 ”的特性恰恰满足了这种需求。所以,怎么从mfc程序迁移到qt上来是很多公司需要考虑的问题。至于从qt迁移到mfc这种需求应该是不会出现的 ,mfc1.0 1992年就发布,qt94年才开始动工呢。怎么说都是mfc历史悠久,地大物博...

选择了qt作为mfc的替代品,那就得考虑是新建一个qt工程重新写一遍还是能在原来的mfc工程里直接使用qt 组件逐步替换掉mfc的部分。本文讨论的是后一种需求。所幸的是,细心的qt官方也考虑到了这一点。官方提供了 Qt/MFC Migration Framework 来实现在mfc工程里混用qt的组件。可以从这个页面找到相关的下载:http://doc.trolltech.com/solutions/qtwinmigrate/winmigrate-walkthrough.html
这个framework比较简单就包括几个类QWinWidget,QMfcApp等。 关于具体怎么做,该页面上也能找到很多实例。翻译成中文大致有这么几点:

首先,要想使用一个QtGui部件,比如QPushButton, QWidget之类的,必须有一个QApplication 对象存在。在创建一个QPushButton时,代码会检测是否有这么一个QApplication对象存在,否则程序会crash掉。所以需要创建一个 QApplication对象:

BOOL WindowsApp::InitInstance()
{  // Standard initialization

#if _MFC_VER < 0x0700
#ifdef _AFXDLL
Enable3dControls();                     // Call this when using MFC in a shared DLL
#else  Enable3dControlsStatic();       // Call this when linking to MFC statically
#endif  #endif

         // Change the registry key under which our settings are stored.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));

         // Qt initialization
MfcApp::instance(this); //这一句就是创建QApplication对象

         MainFrame* pFrame = new MainFrame;
return true;
}

创建了QMfcApp对象,那些QWidget们就有指望了,因为它们的事件需要QApplication 来管理分配。所以,接着就要用QMfcApp对象来接管CWinApp的程序运行机制。
重载CWinApp的Run函数,调用QMfcApp::run(this);
这里用到了上面创建的QMfcApp对象来同时接管QWidget的 event和MFC的message loop. 代码如下:

BOOL WindowsApp::Run()
{  int result = QMfcApp::run(this);
delete qApp;
return result;
}
现在QApplication对象已经有了,new 一个 QPushButton上去,程序也不会报错了。但是,如何让这个QPushButton 能显示在 MFC的窗口类里面呢?
因为构造QPushButton的时候,父类只能接受一个QWidget,而不能不接受一个父类窗口的windows handle。 
QWinWidget登场了!
来看一下QWinWidget的构造函数:
QWinWidget ( HWND hParentWnd, QObject * parent = 0, Qt::WFlags f = 0 );
啥也不用说了,代码如下:

int ChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{  if (CWnd::OnCreate( lpCreateStruct ) == -1 )
return -1;

     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();

     return 0;

懒得翻译了,直接贴一段吧
Now we can create the QWinWidget instance with this CWnd instance as a parent window, and use that instance as a parent to the QWidgets we want to use to create the user interface. Since QWinWidget is a proper QWidget it can be laid out, and we move the Qt GUI to the upper left corner of the MFC child and show() the user interface immediately.
最后别忘了,这个widget还是要干掉的。widget一干掉,下面那些子子孙孙widget全部干掉,qt就这么霸道!

void ChildView::OnDestroy()
{  delete widget;
widget = 0;

     CWnd::OnDestroy();
}
就简单讲到这里。我们已经知道如何让mfc代码和qt代码放在一起编译通过了,剩下的工作就是把mfc部分一点点替换掉了。
任重而道远!
  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值