简体   繁体   English

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

[英]Unknown type name QML_ELEMENT and QML module can't be found even though copied straight from a official example

When I want to run this example of Integrating QML and C++ I get a error (in the backend.h) that QML_ELEMENT is not a defined type.当我想运行这个集成 QML 和 C++ 的示例时,我收到一个错误(在 backend.h 中) QML_ELEMENT不是定义的类型。 I know that a similar question was asked here already, however since I copy pasted from the official example, I did #include <qqml.h> .我知道这里已经问过类似的问题,但是由于我从官方示例中复制粘贴,所以我做了#include <qqml.h> That means the cause of my error has to be a different one.这意味着我的错误的原因必须是不同的。
In addition importing io.qt.examples.backend 1.0 gives me a error, that this QML module can not be found.另外导入io.qt.examples.backend 1.0给我一个错误,这个 QML 模块找不到。 I use Qt 5.9 and Qt Quick Application.我使用 Qt 5.9 和 Qt 快速应用程序。

The content of my backend.cpp, backend.h and main.qml are copy pasted from the example, so I won't repost them.我的backend.cpp、backend.h和main.qml的内容是从示例中复制粘贴的,所以不再转发。 My project file is just the default one with the additions from the example:我的项目文件只是示例中添加的默认文件:

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 is available from Qt >= 5.15, as you are using Qt 5.9 then you must use qmlRegisterType<BackEnd>("io.qt.examples.backend", 1, 0, "BackEnd"); QML_ELEMENT is available from Qt >= 5.15, as you are using Qt 5.9 then you must use qmlRegisterType<BackEnd>("io.qt.examples.backend", 1, 0, "BackEnd"); as points out for Qt 5.9 docs正如Qt 5.9 文档所指出的那样

I was having the same issue you described in Qt 5.15.我遇到了您在 Qt 5.15 中描述的相同问题。 The following fixed the problem for me: In the project directory on disk, find the.qmltypes file that is automatically generated by Qt and add that file to the debug folder (also automatically created by Qt).以下解决了我的问题:在磁盘上的项目目录中,找到由 Qt 自动生成的 .qmltypes 文件并将该文件添加到调试文件夹(也是由 Qt 自动创建的)。 This will make the red error messages in Qt Creator go away.这将使 Qt Creator go 中的红色错误消息消失。 In this blog post , it is mentioned that在这篇文中,提到了

Such static registration of C++ types for QML will not only generate qmltypes files for plugins, but also for your main application.此类 static 注册 QML 的 C++ 类型不仅会为插件生成 qmltypes 文件,还会为您的主要应用程序生成 qmltypes 文件。 The latter files are called "app.qmltypes" and should be placed next to your application binary, the same way "plugins.qmltypes" should be placed next to the plugin they refer to.后面的文件称为“app.qmltypes”,应该放在您的应用程序二进制文件旁边,就像“plugins.qmltypes”应该放在它们引用的插件旁边一样。

So its just an issue of making sure that the.qmltypes file is in the correct place (next to the application binary).因此,它只是确保 .qmltypes 文件位于正确位置(在应用程序二进制文件旁边)的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM