PluginLoader returns a QObject instance, but qobject_cast returns null (0x0). [Solved]
-
Hi,
I have the follow code to load my plugin:
@foreach(QString fileName, pluginsDir.entryList(QDir::Files)) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = pluginLoader.instance();
if (plugin) {
DLinkModemModule *dlinkModule = qobject_cast<DLinkModemModule *>(plugin);
if (dlinkModule) {
moduleInstances[dlinkModule->getModuleName()] = dlinkModule;
modules << dlinkModule->getModuleName();
std::cout << "Module " << dlinkModule->getModuleName().toStdString().c_str() << " - " << dlinkModule->getModuleVersion().toStdString().c_str() << " is loaded." << std::endl;
}
}
}@But the code qobject_cast<DLinkModemModule *>(plugin) returns null. Anyone know this issue?
My interface:
@
#ifndef DLINKMODULE_H
#define DLINKMODULE_H#include <QtCore/QtPlugin>
QT_BEGIN_NAMESPACE
class QBool;
class QString;
class QObject;
QT_END_NAMESPACEclass DLinkModemModule {
public:
/**- Destrutor
*/
virtual ~DLinkModemModule(){};
//...my methods...
};
QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(DLinkModemModule, "br.com.dlink.ModuleManager.DLinkModemModule/1.0")
QT_END_NAMESPACE#endif /* DLINKMODULE_H */
@My header of implementation plugin:
@
#ifndef SUPPORT500B_H
#define SUPPORT500B_H#include "DLinkModemModule.h"
#include <QtCore/QObject>
#include <QtCore/QStringList>class Support500b : public QObject, public DLinkModemModule {
Q_INTERFACES(DLinkModemModule)
public:virtual ~Support500b(){};
// all methods defined in plugin interface
};QT_BEGIN_NAMESPACE
Q_EXPORT_PLUGIN2(support500b, Support500b)
QT_END_NAMESPACE#endif /* SUPPORT500B_H */
@My CPP with Support500b class, has all interface method implemented. Any idea?
- Destrutor
-
Did you get any error?
Be sure that the project build mode "Debug/Release" should be the same while creating the plugin and also while loading the plugin. There is a video tutorial "here":http://www.voidrealms.com/viewtutorial.aspx?id=246. You can also share the .pro file for both the projects (1 creating and 2nd reading/loading).
-
About the error, Qt have any function for read error? Ex:
@
DLinkModemModule *dlinkModule = qobject_cast<DLinkModemModule *>(plugin);
if (dlinkModule) {
...
}else{
std::cout << qobject_cast_error() << std::endl;
}
@But now, no error is raised. Just a null pointer is returned.
I will watch the video and back here to report the results. Thank you Sam!
-
Yes the is one more option to check if the plugin fails to load. You can check for pluginLoader.errorString() .
@DLinkModemModule *dlinkModule = qobject_cast<DLinkModemModule *>(plugin);
if (dlinkModule) {
...
}else{
std::cout << pluginLoader.errorString()<< std::endl;
}@Where pluginLoader is defined as
@QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));@ -
Oh Sam! You are the man!
Summary:
1- I remove Q_OBJECT macro from my plugin implementation header, because this issue http://qt-project.org/forums/viewthread/19343/ , but the real cause was the macros QT_BEGIN_NAMESPACE/QT_END_NAMESPACE.
2- The my reference, was that document http://michael-stengel.com/blog/?p=4 , and many things dont work more.
3- My plugin interface methods were not with the keyword const in the end