MFC向QT移植示例程序Walkthrough(上)
(2011-04-23 10:26:26)
标签:
mfcqt移植it |
分类: QT |
编译环境:
示例程序:
1) 打开MFC示例工程文件,确保编译运行成功。
2)代替MFC消息循环。
-修改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对话框。
-为了采用 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" );
}