【发布时间】:2020-10-18 07:48:13
【问题描述】:
我尝试使用 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")
}
【问题讨论】:
-
直到现在我尝试了很多东西,但总是出现相同的错误消息。他们是下载测试的示例吗?
-
我也面临类似的问题。你找到解决办法了吗?
-
不,我使用旧样式然后注册模块