当我想运行集成 QML 和 C++ 的 this 示例时,我收到一个错误(在 backend.h 中),即 QML_ELEMENT
不是定义的类型。我知道已经有人问过类似的问题 here ,但是由于我从官方示例中复制粘贴,我做了 #include <qqml.h>
。这意味着我的错误的原因必须是不同的。
另外导入 io.qt.examples.backend 1.0
给了我一个错误,找不到这个 QML 模块。我使用 Qt 5.9 和 Qt Quick Application。
我的backend.cpp、backend.h和main.qml的内容是从例子中复制粘贴过来的,我就不转发了。我的项目文件只是带有示例中添加内容的默认文件:
QT += quick
CONFIG += c++11 qmltypes
# 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 += \
backend.cpp \
main.cpp
RESOURCES += qml.qrc
QML_IMPORT_NAME = io.qt.examples.backend
QML_IMPORT_MAJOR_VERSION = 1
# 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 += \
backend.h
最佳答案
QML_ELEMENT
可从 Qt >= 5.15 获得,因为您使用的是 Qt 5.9,那么您必须使用 qmlRegisterType<BackEnd>("io.qt.examples.backend", 1, 0, "BackEnd");
作为 Qt 5.9 docs 的点
关于c++ - 即使直接从官方示例中复制,也找不到未知类型名称 QML_ELEMENT 和 QML 模块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62545830/