qt - Qml Module not found CPP Class registration with new QML_ELEMENT r
问题描述
我尝试使用 Qt5.15.0 和新的宏 QML_ELEMENT 在 QML 中注册我的自定义 CPP 类,但找不到模块。Qt Creater 帮助文件描述了 QML_ELEMENT 的步骤。我也检查了 Qt 手册,但没有愉快的结局(https://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html)。
//testclass.h
#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <QObject>
#include <QtQml>
class testclass : public QObject
{
Q_OBJECT
QML_ELEMENT
public:
explicit testclass(QObject *parent = nullptr);
signals:
};
#endif // TESTCLASS_H
// testclass.cpp
#include "testclass.h"
testclass::testclass(QObject *parent) : QObject(parent)
{
}
pro 文件如下所示:
QT += quick
CONFIG += c++11
CONFIG += qmltypes
QML_IMPORT_NAME = com.mycompany.test
QML_IMPORT_MAJOR_VERSION = 1
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
testclass.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
testclass.h
使用以下内容生成元文件:
[
{
"classes": [
{
"classInfos": [
{
"name": "QML.Element",
"value": "auto"
}
],
"className": "testclass",
"object": true,
"qualifiedClassName": "testclass",
"superClasses": [
{
"access": "public",
"name": "QObject"
}
]
}
],
"inputFile": "testclass.h",
"outputRevision": 67
}
]
之后我尝试在我的 Main.qml 中使用这个类,但 Qt Creator 显示未找到 QML 模块。
// main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import com.mycompany.test 1.0 // QML Module not found
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
}
//Main.cpp
#include "testclass.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
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.load(url);
return app.exec();
}
我错过了什么还是有问题?
** 编辑 **
我将 Classname 从 testclass 重构为 Testclass,但仍然出现相同的错误。当然,我做了清洁和重建。我正在使用 Windows 10、MinGw-64Bit 和 qmake。
//testclass.h
#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <QObject>
#include <QtQml>
class Testclass : public QObject
{
Q_OBJECT
QML_ELEMENT
public:
explicit Testclass(QObject *parent = nullptr);
signals:
};
#endif // TESTCLASS_H
//testclass.cpp
#include "testclass.h"
Testclass::Testclass(QObject *parent) : QObject(parent)
{
}
// Metafile
[
{
"classes": [
{
"classInfos": [
{
"name": "QML.Element",
"value": "auto"
}
],
"className": "Testclass",
"object": true,
"qualifiedClassName": "Testclass",
"superClasses": [
{
"access": "public",
"name": "QObject"
}
]
}
],
"inputFile": "testclass.h",
"outputRevision": 67
}
]
//main.cpp
#include "testclass.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
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.load(url);
return app.exec();
}
//main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import com.mycompany.test 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
}
解决方案
我在尝试使用 MinGW 构建示例代码时遇到了同样的错误。但奇怪的是,在使用 Clang multi-Abi 为 Android 编译后,相同的代码可以完美运行。
您可以尝试使用 QT 5.9 https://doc.qt.io/qt-5.9/qtqml-cppintegration-topic.html中的旧方法
您只需要在 main.cpp 中添加它并包含您的 Testclass 标头
qmlRegisterType<Testclass>("com.mycompany.test", 1, 0, "Testclass")
推荐阅读
- javascript - Firefox 中的 React 性能问题?
- django - Django 需要在一个响应中同时添加两个序列化器数据
- python - 自动化无聊的东西:电话和电子邮件地址提取器错误
- css - woocommerce 产品小部件显示我无法设置的神秘类
- javascript - 在 Vue.js 中,如何滚动到导入的组件?
- python - Python 3 CouchDB 单次更新与批处理
- c# - MVC HTML Helper DropDownListFor 未提交所选值
- debugging - 获取对脚本对象的引用
- pandas - 删除空值后如何重新排序数据帧序列号
- php - 自动将文件从 S3 复制到 EC2