我想编写QML插件来将一些类从Qt暴露给QML层。我按照here的指南进行了操作。这是代码:
expose.pro
TEMPLATE = lib
CONFIG += qt plugin
QT += qml gui
DISTFILES += expose.json
DESTDIR = ../g/Expose
TARGET = expose
SOURCES += expose.cpp
expose.cpp
#include <QQmlExtensionPlugin>
#include <QQmlEngine>
#include <QGuiApplication>
class QGuiApplicationWrapper : public QGuiApplication {
int argc;
public:
QGuiApplicationWrapper() : QGuiApplication(argc, nullptr) {}
};
class QExampleQmlPlugin : public QQmlExtensionPlugin {
Q_OBJECT
Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
public:
void registerTypes(const char *uri) {
Q_ASSERT(uri == QLatin1String("g.Expose"));
qmlRegisterType<QGuiApplicationWrapper>(uri, 1, 0, "GuiApplication");
}
};
但是当我从QML尝试import g.Expose 1.0
时,会出现以下错误:
plugin cannot be loaded for module "g.Expose": Failed to extract plugin meta data from 'g/Expose/libexpose.so
我查看了加载器的代码( qlibrary.cpp ):
bool ret = false;
if (pos >= 0) {
if (hasMetaData) {
const char *data = filedata + pos;
QJsonDocument doc = QLibraryPrivate::fromRawMetaData(data);
lib->metaData = doc.object();
if (qt_debug_component())
qWarning("Found metadata in lib %s, metadata=\n%s\n",
library.toLocal8Bit().constData(), doc.toJson().constData());
ret = !doc.isNull();
}
}
if (!ret && lib)
lib->errorString = QLibrary::tr("Failed to extract plugin meta data from '%1'").arg(library);
所以我想我必须在Q_PLUGIN_METADATA
中提供并指定一些json,但我在文档中找不到描述其格式和包含程序的地方