打开VS2008创建一个dll项目(创建了一个QT Library项目),新建头文件q4dapplication.h定义纯虚类:
#ifndef Q4DAPPLICATION
#define Q4DAPPLICATION
class Q4DApplication
{
public:
virtual int exec() = 0;
};
#endif
新建头文件q4dapplicationimpl.h实现纯虚类:
#ifndef Q4DAPPLICATIONIMPL
#define Q4DAPPLICATIONIMPL
#include <QApplication>
#include "q4dapplication.h"
class Q4DApplicationImpl : public Q4DApplication, private QApplication
{
public:
Q4DApplicationImpl(int argc, char **argv);
~Q4DApplicationImpl();
int exec();
};
#endif
在VS项目自动生成的导出类的头文件中加入:
extern "C" __declspec(dllexport) Q4DApplication* NewQ4DApplication(int argc, char **argv);
在对应的.cpp文件中加入:
__declspec(dllexport) Q4DApplication* NewQ4DApplication(int argc, char **argv)
{
return new Q4DApplicationImpl(argc, argv);
}
编译出DLL给Delphi调用.
Delphi端导入单元如下:
unit ImptQTClass;
interface
uses SysUtils;
type
TQ4DApplication = class
public
function exec: Integer; virtual; cdecl; abstract;
end;
const QTDLL = 'Q4DLibrary.dll';
function NewQ4DApplication(AArgc: Integer; AArgv: PPChar): TQ4DApplication; cdecl;
implementation
function NewQ4DApplication; external QTDLL;
end.
测试代码:
var
oQApp: TQ4DApplication;
p: PChar;
i: Integer;
begin
p := '';
oQApp := NewQ4DApplication(0, @p);
i := oQApp.exec();
Assert(i = 1234);
本想尝试导出QT的GUI库给Delphi调用呢,发现QApplication创建后调用其他QWidget的Show方法有问题,估计和Delphi的Application对象冲突吧,失败了,以后在查找原因.不过感觉给Delphi导出GUI库没啥必要,纯属为了测试.可以将一些非界面相关的库导出来用,以后有空在继续吧.
联系客服