我现在正在尝试动态地包含类文件,并选择通过将.dll加载到QLibrary中来实现。我现在遇到的问题是,当我试图调用-method时,它会返回0。
编辑:在此期间,问题已经解决,我决定编辑代码,以便其他人可以看到它是如何工作的:
这是.dll´s头文件:
#ifndef DIVFIXTURE_H
#define DIVFIXTURE_H
#include<QObject>
#include<QVariant>
class __declspec(dllexport) DivFixture : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE DivFixture();
Q_INVOKABLE void setNumerator(QVariant num);
Q_INVOKABLE void setDenominator(QVariant denom);
Q_INVOKABLE QVariant quotient();
private:
double numerator, denominator;
};
#endif这是dll的..cpp file:
#include "testfixture.h"
DivFixture::DivFixture(){}
void DivFixture::setNumerator(QVariant num)
{
numerator=num.toDouble();
}
void DivFixture::setDenominator(QVariant denom)
{
denominator=denom.toDouble();
}
QVariant DivFixture::quotient()
{
QVariant ret;
ret=numerator/denominator;
return ret;
}
//non-class function to return pointer to class
extern "C" __declspec(dllexport) DivFixture* create()
{
return new DivFixture();
}--我就是这样加载类的:
currentFixture.setFileName("C:\\somepath\\testFixture.dll");
if(currentFixture.load());
{
typedef QObject* (*getCurrentFixture)();
getCurrentFixture fixture=(getCurrentFixture)currentFixture.resolve("create");
if (fixture)
{
Fixture=fixture();
}
}发布于 2011-10-06 08:32:16
您需要使用解密规范(Dllexport)导出类
class __declspec(dllexport) DivFixture : public QObject
{发布于 2016-11-24 15:52:53
接受的答案是不正确的。__declspec有两个可能的参数:
编译库时使用dllexport,链接到库时使用dllimport。
Qt已经为此提供了定义:
为了适当地利用它们,增加如下内容:
#if defined(MYSHAREDLIB_LIBRARY)
# define MYSHAREDLIB_EXPORT Q_DECL_EXPORT
#else
# define MYSHAREDLIB_EXPORT Q_DECL_IMPORT
#endif到项目中的全局标题,该标题将包含在要导出的所有类中。然后修改类,最后声明如下:
class MYSHAREDLIB_EXPORT DivFixture : public QObject在Qt文档的创建共享图书馆中给出了一个完整的示例和更多信息。
https://stackoverflow.com/questions/7671856
复制相似问题