Here is my plugin file:
#include <QQmlExtensionPlugin>
class VersionInterface
{
public:
virtual const QString version() const = 0;
virtual ~VersionInterface() {}
};
#define VersionInterface_iid "com.fx.VersionInterface/1.0"
Q_DECLARE_INTERFACE(VersionInterface, "com.fx.VersionInterface")
class FXQLLauncherPlugin : public QQmlExtensionPlugin, public VersionInterface
{
Q_OBJECT
Q_INTERFACES(VersionInterface)
Q_PLUGIN_METADATA(IID "com.fx.QLLauncherPlugin")
public:
void registerTypes(const char *uri);
void initializeEngine(QQmlEngine *engine, const char *uri);
const QString version() const final override;
};
It compiles fine.
However in the application where I tried to load and cast the object qobject_cast
returns nullptr
:
qmlPlugin.load();
if (qmlPlugin.isLoaded()) {
QObject *rootObject = qmlPlugin.instance();
// rootObject is valid object
qDebug() << rootObject->inherits("QQmlExtensionPlugin");
// outputs: true
qDebug() << rootObject->inherits("VersionInterface");
// outputs: false
VersionInterface* plugin = qobject_cast<VersionInterface*>(rootObject);
// plugin == nullptr
}
MOC-file generates valid code: moc_plugin.cpp
void *FXQLLauncherPlugin::qt_metacast(const char *_clname)
{
if (!_clname) return Q_NULLPTR;
if (!strcmp(_clname, qt_meta_stringdata_FXQLLauncherPlugin.stringdata))
return static_cast<void*>(const_cast< FXQLLauncherPlugin*>(this));
if (!strcmp(_clname, "VersionInterface"))
return static_cast< VersionInterface*>(const_cast< FXQLLauncherPlugin*>(this));
if (!strcmp(_clname, "com.fx.VersionInterface"))
return static_cast< VersionInterface*>(const_cast< FXQLLauncherPlugin*>(this));
return QQmlExtensionPlugin::qt_metacast(_clname);
}
Please explain why qobject_cast
does not work here?