简体   繁体   English

从 QML 中的子文件夹集成 C++ 类

[英]Integrating C++ class from subfolder in QML

I am currently working on a c++ integration to QML and so far everything worked fine.我目前正在将 C++ 集成到 QML,到目前为止一切正常。 I got my backend classes exposed and working.我让我的后端类暴露并工作。 Now that I expanded my application I wanted to split my c++ backend into subfolders to have a better project overview.现在我扩展了我的应用程序,我想将我的 c++ 后端拆分为子文件夹,以获得更好的项目概览。 Now I'm running into linking issues where the backend files which are in those subfolders can not be found anymore.现在我遇到了无法再找到这些子文件夹中的后端文件的链接问题。 Since I am using some libraries that require a certain compiler I am using MSVC 16 and it throws the following error:由于我使用了一些需要特定编译器的库,因此我使用的是 MSVC 16,它会引发以下错误:

Cannot open include file: 'loginstate.h': No such file or directory. 

The parsing works fine and QtCreator does not mark the include as wrong.解析工作正常,QtCreator 不会将包含标记为错误。

When building, it creates a file called application.metatypes.json .构建时,它会创建一个名为application.metatypes.json的文件。 In said file all the information about the exposed classes is stored.在所述文件中,存储了有关公开类的所有信息。 When reading through it, I found that loginstates.h is supposed to be in the root directory where it actually is not, it is in a subfolder of root.在阅读它时,我发现 loginstates.h 应该在它实际上不在的根目录中,它在 root 的子文件夹中。 When i change it manually to the proper folder, my application runs.当我手动将其更改为正确的文件夹时,我的应用程序就会运行。

Is there a way to get qmake/moc to generate the proper path for classes in subfolders?有没有办法让 qmake/moc 为子文件夹中的类生成正确的路径? What setting am I missing?我缺少什么设置?

The .pro file: .pro 文件:

QT = core quick serialport network virtualkeyboard

TEMPLATE = app

CONFIG += qt c++17 warn_on qmltypes
CONFIG -= debug_and_release debug_and_release_target

SOURCES += \
    main.cpp \
    datetimebackend.cpp \
    footerbackend.cpp \
    labelbuttonbackend.cpp \
    globaluisettings.cpp \
    headerbackend.cpp \
    scannerbuttonbackend.cpp \
    sidebarbackend.cpp

HEADERS += \
    businessLogic/loginstate.h \
    datetimebackend.h \
    footerbackend.h \
    globaluisettings.h \
    headerbackend.h \
    labelbuttonbackend.h \
    scannerbuttonbackend.h \
    sidebarbackend.h

RESOURCES += qml.qrc

RC_ICONS = ./images/icon.ico

INCLUDEPATH += $$PWD/..

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH += $$PWD

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

QML_IMPORT_NAME = Backend
QML_IMPORT_MAJOR_VERSION = 1

DISTFILES += some qml files

You get those errors just because some header files are not in path and the compiler cannot find them.你得到这些错误只是因为一些头文件不在路径中并且编译器找不到它们。 As you said that you restructured the directory template, you will only need to add appropriate include paths to the INCLUDEPATH parameter in your .pro file like :正如您所说,您重组了目录模板,您只需要向.pro文件中的INCLUDEPATH参数添加适当的包含路径,例如:

INCLUDEPATH += $$PWD/new/include/path

Do it for every directory where contains a .h file that is not being found by the compiler.对包含编译器未找到的.h文件的每个目录执行此操作。

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

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