Finally, I came up with a solution that now works after several hours of work.
The QLibrary class allows you to load DLL files dynamically, so all I had to do was put my class in a .dll and add a function that returns a pointer to the required class.
This is the header .dll file:
#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
This is cpp.dll file:
#include "testfixture.h" DivFixture::DivFixture(){} extern "C" __declspec(dllexport) void DivFixture::setNumerator(QVariant num) { numerator=num.toDouble(); } extern "C" __declspec(dllexport) void DivFixture::setDenominator(QVariant denom) { denominator=denom.toDouble(); } extern "C" __declspec(dllexport) 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(); }
and so I load my class:
currentFixture.setFileName("C:\\somepath\\testFixture.dll"); if(currentFixture.load()); { typedef QObject* (*getCurrentFixture)(); getCurrentFixture fixture=(getCurrentFixture)currentFixture.resolve("create"); if (fixture) { Fixture=fixture(); } }
After that, I can get a QMetaObject and call any method that I like. Hope this helps those facing a similar problem in the future.
source share