Context: I am currently working on a qmlextensionplugin with a c++ backend. The plugin manages a QList and instantiates for each entry a qml visualisation. I was already able to make the list fully functional in qml.
Problem: The last thing I am missing is to update the list via c++. For that I came across this thread, so I tried to follow this example. Unfortunately I can´t even start the application due to the following exceptions.I marked the occurence location in the source code.
Why am I getting those exceptions and how can I fix them?
Snippet of the source code:
Here my implementation to load the plugin in main.cpp:
#include "NameListInterface.h"
#include <QGuiApplication>
#include <QPluginLoader>
#include <QQmlApplicationEngine>
#include <QLibraryInfo>
#include <QDebug>
void LoadPlugin(){
NameListInterface* pluginVersion = nullptr;
#ifdef Q_OS_MACOS
const QString fileSuffix = ".dylib";
#else
const QString fileSuffix = ".dll";
#endif
QPluginLoader qmlPlugin(path + "NameList/NameListPlugin.dll");
qmlPlugin.load();
if (qmlPlugin.isLoaded()) {
pluginVersion = qobject_cast<NameListInterface*>(qmlPlugin.instance());//**EXCEPTION OCCURS HERE**
} else {
qmlPlugin.setFileName(QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath) + "/fx/my/libmyplugin" + fileSuffix);
qmlPlugin.unload();
qmlPlugin.load();
if (qmlPlugin.isLoaded()) {
pluginVersion = qobject_cast<NameListInterface*>(qmlPlugin.instance());
} else {
qDebug() << "ERROR while opening plugin: " << qmlPlugin.errorString();
}
}
if (pluginVersion) {
qDebug() << "Plugin: \n" << pluginVersion->GetVersion() << "\n"
<< "Location: " << qmlPlugin.fileName();
} else {
qDebug() << "Can't obtain version information from the plugin";
}
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QString path = "C:/Workspace/QT/QMLImport/qml/";
LoadPlugin(path);
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.addImportPath(path);
engine.load(url);
return app.exec();
}
Here the Plugin´s header file including NameListInterface.h:
#ifndef NAMELISTPLUGIN_H
#define NAMELISTPLUGIN_H
#include <QObject>
#include<QQmlApplicationEngine>
#include "Person.h"
#include <QtQuick/QQuickView>
#include <QGuiApplication>
#include <QDebug>
#include <QQmlExtensionPlugin>
#include<QQmlContext>
class NameListInterface{
public:
virtual const QString GetVersion() const = 0;
virtual ~NameListInterface() {}
};
#define NameListInterface_iid "com.NameListPlugin"
Q_DECLARE_INTERFACE(NameListInterface, "com.my.NameListInterface")
class NameListPlugin : public QQmlExtensionPlugin, public NameListInterface
{
Q_OBJECT
Q_INTERFACES(NameListInterface)
Q_PLUGIN_METADATA(IID NameListInterface_iid)
public:
explicit NameListPlugin(QObject *parent = nullptr);
void initializeEngine(QQmlEngine *engine, const char *uri);
void registerTypes(const char *uri);
// interface Implementation
const QString GetVersion() const;
// QML Extension plugin implementation
//...
#endif // NAMELISTPLUGIN_H
