c++ - 即使直接从官方示例复制,也找不到未知类型名称 QML_ELEMENT 和 QML 模块

标签 c++ qt qml

当我想运行 this集成 QML 和 C++ 的示例我得到一个错误(在 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/

相关文章:

windows - QML 缩放不适用于非整数缩放因子

c++ - 仅 header 库中匿名命名空间的替代方案

c++ - 如何将多维动态数组传递给 C++ 中的函数

qt - 保存 QGraphicsScene 图像

qt - QML ListView 隐藏委托(delegate)项

c++ - QML:如何从 C++ 代码正确地将属性传递给 PluginParameter 值?

qml - QML 中的多点触摸

c++ - 重载 C++ 赋值运算符

c++ - 为什么我会收到编译错误 "use of deleted function ' std::unique_ptr ...”

c++ - QTimer setInterval 不重置 remainingTime