加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

MFC向QT移植示例程序Walkthrough(上)

(2011-04-23 10:26:26)
标签:

mfc

qt

移植

it

分类: QT

编译环境:

    -VS2005

    -QT4.7.2

示例程序:

    -qtwinmigrate\examples\mfc\step1

 

1) 打开MFC示例工程文件,确保编译运行成功。

   http://blog.sina.com.cn/s/blog_539d07840100qols.html

   本例为加载外部DLL动态链接库函数,显示一个版本信息对话框。

2)代替MFC消息循环。

   Windows程序移植框架提供的QMfcApp(QApplication子类)合并了Qt和MFC消息循环。

   移植首先需要重新实现MFC WindowsApp类中的Run()函数:

-修改qtmfc.h头文件中虚函数定义

class WindowsApp : public CWinApp
{
public:
         WindowsApp();

// Overrides
         // ClassWizard generated virtual function overrides
         //{{AFX_VIRTUAL(WindowsApp)
         public:
         virtual BOOL InitInstance();
         virtual BOOL Run();
         //}}AFX_VIRTUAL

// Implementation

public:
         //{{AFX_MSG(WindowsApp)
         afx_msg void OnAppAbout();
         //}}AFX_MSG
         DECLARE_MESSAGE_MAP()
};

 

-修改qtmfc.cpp源文件中函数实现

#include "stdafx.h"
#include "qtmfc.h"

#include "mainframe.h"

#include <qmfcapp.h>

BOOL WindowsApp::Run()
{
     return QMfcApp::run( this );
}

3)代替MFC对话框。

   代替MFC中引用外部Qt plugin DLL实现的对话框,直接在源码中实现。具体可通过Qt Designer来生成一个QDialog,或者通过 KDAB's KNUT 工具将Microsoft源文件中原有的对话框转换为.ui文件。现直接采用QT的QtMessageBox::about() 函数来实现。

-为了采用 QMessageBox API, 必须包含相应的头文件,另外QMessageBox作为MFC主窗口的子窗口,还需要在头文件中包含QWinWidget类。

#include "stdafx.h"
#include "qtmfc.h"

#include "mainframe.h"

#include <qmfcapp.h>
#include <qwinwidget.h>
#include <QtGui/QMessageBox>

 

-在栈中生成QWinWidget 物体,用MFC主窗口作为父窗口,showCentered() API 确保QWinWidget的子窗Qt message box消息框在主窗口的中央打开。

// WindowsApp message handlers

// App command to run the dialog
void WindowsApp::OnAppAbout()
{
     QWinWidget win( theApp.m_pMainWnd );
     win.showCentered();

QTextCodec *codec = QTextCodec::codecForName("System");    //获取系统编码
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);

// 另外一种中文乱码解决方法
// QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
// QFont font("微软雅黑",12,QFont::Normal,FALSE);
// win.setFont(font);

// QMessageBox::about(&win, "About QtMfc",QObject::tr("QtMfc 版本 Version 1.0\n Copyright (c) 2011"));
QMessageBox::about( &win, "About QtMfc", "QtMfc 版本 Version 1.0\nCopyright (C) 2011" );
}

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有