QLibrary resolve returns false
QLibrary resolve returns false
我现在正试图动态地包含类文件,并选择通过将.dll加载到QLibrary中来这样做。我现在遇到的问题是,当我尝试调用resolve()方法时,它返回0.
编辑:与此同时,问题已经解决,我决定编辑代码,这样其他人就可以看到它是如何工作的:
这是.dll的头文件:
#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的。pcp文件:
#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();
}
}
您需要使用__declspec(dllexport)导出您的类
class __declspec(dllexport) DivFixture : public QObject
{
接受的答案不正确。__declspec
有两个可能的参数:
- dllexport
- dllimport
编译库时使用dllexport,链接库时使用dllimport。
Qt已经提供了相应的定义:
- Q_DECL_EXPORT
- Q_DECL_IMPORT
要正确使用它们,请添加如下内容:
#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的文档中创建共享库给出了一个完整的示例和更多信息。
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 伪造事实↔false
- QueryWorkingSet总是返回false
- 如何区分地图中的 0 和 false?
- 模板参数列表中的 false 在模板初始化期间计算为什么?
- Arduino-C++ bool 不会从 false 变为 true
- 当返回类型声明为 ListNode 时,我们是否可以返回 false<T>*
- 当我的 if 条件计算结果为 false 时,我的 else 块将不会执行
- 等于在 c++ 中返回 false
- strcmp 对于相等的字符序列返回 false
- std::is_class 在引用类上为 false
- ios_base::sync_with_stdio(false); cin.tie(NULL);
- CreateWICTextureFromFile returns E_NOINTERFACE
- C++ - 负数和正数之间的比较返回 false
- 如何使用boost定义布尔类,可能的值应该是TRUE或FALSE?
- 如何编写一个接受如下断言消息的自定义断言函数:assert(false) << "assertio
- QSslSocket and supportsSsl() returns false
- setData returns false for QAbstractProxyModel
- Qfile open returns false
- QLibrary resolve returns false