我正在尝试使用CMake创建简单的QML C++插件。以下是我的来源:
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.12)
project(qmltest LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
# Path to installed Qt 5.15
set(Qt5Core_DIR ".../Qt/5.15.0/gcc_64/lib/cmake/Qt5Core")
set(Qt5Qml_DIR ".../Qt/5.15.0/gcc_64/lib/cmake/Qt5Qml")
set(Qt5Quick_DIR ".../Qt/5.15.0/gcc_64/lib/cmake/Qt5Quick")
find_package(Qt5 COMPONENTS Core Quick Qml REQUIRED)
LINK_DIRECTORIES(.../Qt/5.15.0/gcc_64/lib)
INCLUDE_DIRECTORIES(.../Qt/5.15.0/gcc_64/include)
INCLUDE_DIRECTORIES(.../Qt/5.15.0/gcc_64/include/QtQml)
set(QML_IMPORT_PATH ${CMAKE_CURRENT_BINARY_DIR} CACHE STRING "" FORCE)
add_library(dummy SHARED
"dummy.h" "dummy.cpp" "myplugin.h" "myplugin.cpp"
)
target_link_libraries(dummy Qt5::Core Qt5::Quick Qt5::Qml)
add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc")
target_link_libraries(${PROJECT_NAME} dummy Qt5::Core Qt5::Quick Qt5::Qml)
main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QObject>
#include <QtPlugin>
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
伪.h:
#ifndef DUMMY_H
#define DUMMY_H
#include <QObject>
class Dummy : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
public:
explicit Dummy(QObject *parent = nullptr);
QString name();
void setName(const QString x);
signals:
void nameChanged();
public slots:
private:
QString _name;
};
#endif // DUMMY_H
myplugin.h:
#ifndef MYPLUGIN_H
#define MYPLUGIN_H
#include <QtQml/QQmlEngineExtensionPlugin>
#include <QObject>
#include <QQmlApplicationEngine>
#include <iostream>
#include <dummy.h>
class MyPlugin : public QQmlEngineExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid)
public:
void registerTypes(const char* uri) {
std::cout << "GGGGGGGGGGGGGGGGn";
qmlRegisterType<Dummy>(uri, 1, 0, "Dummy");
qmlRegisterModule(uri, 1, 0);
qmlProtectModule(uri, 1);
}
};
#endif // MYPLUGIN_H
main.qml:
import MyPlugin 1.0
Window {
Dummy {}
}
我还在我的构建目录中创建了名为MyPlugin的目录,并链接到构建的libdummy.so和qmldir文件,如下所示:
module MyPlugin
plugin dummy
项目构建,但当我运行它时,我得到了一个错误:
plugin cannot be loaded for module "MyPlugin": Cannot protect module MyPlugin 1 as it was never registered
registerTypes
函数的调试输出尚未被调用(stdout上没有输出(,因此我认为由于某些原因,类型尚未注册。如果我尝试通过将此行添加到myplugin.cpp:来静态注册类型
static int unused = qmlRegisterType<Dummy>("MyPlugin", 1, 0, "Dummy");
,我得到一个错误:
plugin cannot be loaded for module "MyPlugin": Namespace 'MyPlugin' has already been used for type registration
我在这个插件中做错了什么?如何使它与CMake一起工作?
Qt包含两个不同的qml插件基类:QQmlEngineExtensionPlugin(这个类是在Qt 5.14中引入的(和QQml扩展插件
myplugin.h:
#ifndef MYPLUGIN_H
#define MYPLUGIN_H
#include <QtQml/QQmlExtensionPlugin>
#include "dummy.h"
class MyPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid)
public:
void registerTypes(const char* uri) {
qmlRegisterType<Dummy>(uri, 1, 0, "Dummy");
}
};
#endif // MYPLUGIN_H
从Qt 6.2开始,插件类由qt6_ad_qml_module为您生成:
https://doc.qt.io/qt-6/cmake-build-reusable-qml-module.html