PDA

View Full Version : how create CWnd and use it in Qt



litterflybug
15th September 2009, 07:11
Hi: every one
i'm want use crystal report in qt,i find Qt/MFC Migration Framework ,and test example.
i proble is
i have a wrap class from crystal report control,this is deriver from CWnd
how can i complie it with Qt Application.


//////////////////////////
//h
#ifndef CRVIEWER1_H
#define CRVIEWER1_H

#include "stdafx.h"

class CCrviewer1 : public CWnd
{
protected:
DECLARE_DYNCREATE(CCrviewer1)
public:
............
//

};
#endif
////////////////////////////////////////
//cpp

//#include "stdafx.h"
#include "crviewer1.h"

/////////////////////////////////////////////////////////////////////////////
// CCrviewer1

IMPLEMENT_DYNCREATE(CCrviewer1, CWnd)

///////////////////////////////////////////////////////
//MyCtrl.h
#ifndef MYCTRL_H
#define MYCTRL_H
#import "C:\\Program Files\\Common Files\\Crystal Decisions\\2.0\\bin\\craxddrt9.dll" no_namespace
#include "crviewer1.h"
#include <qwinhost.h>
#include <QtGui>

class HostWindow : public QWinHost
{
Q_OBJECT
public:
HostWindow(QWidget *parent = 0, Qt::WFlags f = 0)
: QWinHost(parent, f)
{
setFocusPolicy(Qt::StrongFocus);
}

HWND createWindow(HWND parent, HINSTANCE instance)
{
//how can i create CCrviewer1???
return 0;
}

signals:
void message(const QString &msg, int timeout);

public slots:
void returnPressed()
{
QMessageBox::information(topLevelWidget(), "Message from Qt", "Return pressed in QLineEdit!");
}
public:
CCrviewer1* m_pCRViewer1;
IApplicationPtr m_Application;
IReportPtr m_Report;

protected:

};
#endif


then i compile the application,i get this info
c:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include\afxv_w32.h(16) : fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>

i adjest include head sequece ,the complie is ok ,but i get some warning
1>LINK : warning LNK4098: default libraries “mfc90d.lib” conflicts with other libraries;please use /NODEFAULTLIB:library
1>LINK : warning LNK4098: default libraries “mfc90d.lib” conflicts with other libraries;please use /NODEFAULTLIB:library

this i run the program, but the main entry is to AfxWinMain, is not my Qt Application
main()!!



i want to know how can i use CWnd in Qt

thanks every much!

litterflybug
15th September 2009, 10:10
i tried this ,it worked

i set compiler project->property->linker->Advanced->entry
i set entry to main (this is my Qt application's entry), then rebuild,
it ok,and run it ok!

but i did't understand how Qt application link to mfc

someone can explain for this?

litterflybug
15th September 2009, 10:17
#ifndef MYCTRL_H
#define MYCTRL_H
#include "crviewer1.h"
#import "C:\\Program Files\\Common Files\\Crystal Decisions\\2.0\\bin\\craxddrt9.dll" no_namespace

#include <qwinhost.h>
#include <QtGui>



class HostWindow : public QWinHost
{
Q_OBJECT
public:
HostWindow(QWidget *parent = 0, Qt::WFlags f = 0)
: QWinHost(parent, f)
{
setFocusPolicy(Qt::StrongFocus);
}
~HostWindow()
{
if(m_pCRViewer1)
{
m_pCRViewer1->DestroyWindow();
delete m_pCRViewer1;
}
}

HWND createWindow(HWND parent, HINSTANCE instance)
{
CString nn("hello");

CWnd* cwnd=CWnd::FromHandle(parent);
CRect rect;
cwnd->GetClientRect(&rect);

DWORD nStyle=cwnd->GetStyle();

AfxEnableControlContainer();
m_pCRViewer1=new CCrviewer1();
m_pCRViewer1->Create((LPCTSTR)nn, WS_CHILD | WS_VISIBLE , rect, cwnd, 10001 );
m_Application.CreateInstance (__uuidof(Application));
//获取m_Report变量
//staff.rpt为通过向导建立的报表文件,数据库采用SQL Server 7.0
m_Report =m_Application->OpenReport ("World Sales Report.rpt");
//设置报表标题
m_Report->put_ReportTitle (_bstr_t("Title"));

m_pCRViewer1->put_ReportSource(m_Report);
m_pCRViewer1->put_DisplayToolbar (TRUE);
m_pCRViewer1->put_DisplayGroupTree (FALSE);
m_pCRViewer1->put_DisplayBorder (FALSE);
//刷新数据
m_pCRViewer1->Refresh ();
//显示报表内容
m_pCRViewer1->ViewReport();
return m_pCRViewer1->m_hWnd;
}

signals:
void message(const QString &msg, int timeout);

public slots:
void returnPressed()
{
QMessageBox::information(topLevelWidget(), "Message from Qt", "Return pressed in QLineEdit!");
}
public:
CCrviewer1* m_pCRViewer1;
IApplicationPtr m_Application;
IReportPtr m_Report;

protected:
/*void resizeEvent(QResizeEvent *e)
{
QWinHost::resizeEvent(e);

if (m_pCRViewer1)
m_pCRViewer1->MoveWindow(0,0,width(),height());
}*/

};

#endif

this is lastest code,it worked !
but i have other problem , How can i use MFC class dirct in Qt, and do not to reset main enty? and My Usage is correct ?

litterflybug
15th September 2009, 10:26
#ifndef MYCTRL_H
#define MYCTRL_H
#include "crviewer1.h"
#import "C:\\Program Files\\Common Files\\Crystal Decisions\\2.0\\bin\\craxddrt9.dll" no_namespace

#include <qwinhost.h>
#include <QtGui>



class HostWindow : public QWinHost
{
Q_OBJECT
public:
HostWindow(QWidget *parent = 0, Qt::WFlags f = 0)
: QWinHost(parent, f)
{
setFocusPolicy(Qt::StrongFocus);
}
~HostWindow()
{
if(m_pCRViewer1)
{
m_pCRViewer1->DestroyWindow();
delete m_pCRViewer1;
}
}

HWND createWindow(HWND parent, HINSTANCE instance)
{
CString nn("hello");

CWnd* cwnd=CWnd::FromHandle(parent);
CRect rect;
cwnd->GetClientRect(&rect);

DWORD nStyle=cwnd->GetStyle();

AfxEnableControlContainer();
m_pCRViewer1=new CCrviewer1();
m_pCRViewer1->Create((LPCTSTR)nn, WS_CHILD | WS_VISIBLE , rect, cwnd, 10001 );
m_Application.CreateInstance (__uuidof(Application));
//get m_Report
//
m_Report =m_Application->OpenReport ("World Sales Report.rpt");
//set report title
m_Report->put_ReportTitle (_bstr_t("Title"));

m_pCRViewer1->put_ReportSource(m_Report);
m_pCRViewer1->put_DisplayToolbar (TRUE);
m_pCRViewer1->put_DisplayGroupTree (FALSE);
m_pCRViewer1->put_DisplayBorder (FALSE);
//
m_pCRViewer1->Refresh ();
//
m_pCRViewer1->ViewReport();
return m_pCRViewer1->m_hWnd;
}

signals:
void message(const QString &msg, int timeout);

public slots:
void returnPressed()
{
QMessageBox::information(topLevelWidget(), "Message from Qt", "Return pressed in QLineEdit!");
}
public:
CCrviewer1* m_pCRViewer1;
IApplicationPtr m_Application;
IReportPtr m_Report;

protected:
/*void resizeEvent(QResizeEvent *e)
{
QWinHost::resizeEvent(e);

if (m_pCRViewer1)
m_pCRViewer1->MoveWindow(0,0,width(),height());
}*/

};

#endif

this is lastest code,it worked !
but i have other problem , How can i use MFC class dirct in Qt, and do not to reset main enty? and My Usage is correct ?

litterflybug
15th September 2009, 12:07
i tried more, i set Ignore defualt libary,mfc90d.lib;mfcs90d.lib, then build run

it worked ,i do not set main entry:D:D:D

lygstate
11th July 2013, 12:20
I am facing the same problem with you,
can you give me a detailed explain for me?
I am also want to use CWnd as a QT QWidget without change the entrypoint from main to AfxWinMain.