使用 CMake 将 C++ 元素导入 QML 时出现错误的 Qt Creator 警告

问题描述 投票:0回答:1

我按照从 C++ 定义 QML 类型 — 注册可实例化对象类型来创建基本的 C++ 自定义 QML 元素,然后在

Main.qml
中使用它。但是,我收到警告,提示导入失败,特别是这 4 个:

D:\Downloads\CppIntegrationTest\Main.qml:2:警告:导入模块“CppIntegrationTest”时发生警告:[导入]

D:\Downloads\CppIntegrationTest\Main.qml:1:警告:无法导入 CppIntegrationTest。您的导入路径设置正确吗? [导入]

D:\Downloads\CppIntegrationTest\Main.qml:10:警告:未找到 Foo。您是否添加了所有导入和依赖项?:您的意思是“流程”吗? [导入]

D:\Downloads\CppIntegrationTest\Main.qml:10:警告:使用了 Foo 类型,但未解析 [未解析类型]

这些警告并没有破坏构建,但它们使开发变得更加困难。我该如何解决这些警告?

Qt Creator 版本是 14.0.2。基于 Qt 6.7.3。我使用的是 Windows 10。

文件结构:

CppIntegrationTest
|-- CMakeLists.txt
|-- foo.cpp
|-- foo.h
|-- main.cpp
|-- Main.qml

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(CppIntegrationTest VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)

qt_standard_project_setup(REQUIRES 6.5)

qt_add_executable(appCppIntegrationTest
    main.cpp
)

qt_add_qml_module(appCppIntegrationTest
    URI CppIntegrationTest
    VERSION 1.0
    QML_FILES
        Main.qml
        SOURCES foo.h foo.cpp
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appCppIntegrationTest PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appCppIntegrationTest
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_link_libraries(appCppIntegrationTest
    PRIVATE Qt6::Quick
)

include(GNUInstallDirs)
install(TARGETS appCppIntegrationTest
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

foo.h

#ifndef FOO_H
#define FOO_H

#include <QObject>
#include <QQmlEngine>

class Foo : public QObject
{
    Q_OBJECT
    QML_ELEMENT
public:
    explicit Foo(QObject *parent = nullptr);

signals:
};

#endif // FOO_H

foo.cpp

#include "foo.h"

Foo::Foo(QObject *parent)
    : QObject{parent}
{}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreationFailed,
        &app,
        []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    engine.loadFromModule("CppIntegrationTest", "Main");

    return app.exec();
}

Main.qml

import QtQuick
import CppIntegrationTest

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Foo {

    }
}
c++ qt cmake qml qt-creator
1个回答
0
投票

警告准确。

导入找不到模块 CppIntegrationTest,因为这是 CMake 项目的名称。

您生成的模块称为 appCppIntegrationTest。解决这个问题最简单的方法是将模块名称也更改为 CppIntegrationTest (不会有冲突,但如果您愿意,您也可以重命名 CMake 项目名称)

© www.soinside.com 2019 - 2024. All rights reserved.