c++qtcmakeqmlqt-creator

False Qt Creator warnings when importing a C++ element into QML with CMake


I'm following Defining QML Types from C++ — Registering an Instantiable Object Type to create a basic C++ custom QML element and then use it in Main.qml. However, I'm getting warnings suggesting that the import is failing, specifically these 4:

D:\Downloads\CppIntegrationTest\Main.qml:2: warning: Warnings occurred while importing module "CppIntegrationTest": [import]

D:\Downloads\CppIntegrationTest\Main.qml:1: warning: Failed to import CppIntegrationTest. Are your import paths set up properly? [import]

D:\Downloads\CppIntegrationTest\Main.qml:10: warning: Foo was not found. Did you add all imports and dependencies?: Did you mean "Flow"? [import]

D:\Downloads\CppIntegrationTest\Main.qml:10: warning: Type Foo is used but it is not resolved [unresolved-type]

These warnings are not breaking the build, but they make it much harder to develop. How can I resolve these warnings?

The Qt Creator version is 14.0.2. Based on Qt 6.7.3. I'm using Windows 10.

File structure:

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 {

    }
}

Solution

  • The warnings seem to be coming from QMLLS (QML Language Server). The QML Language Server is part of the Qt framework and aims to help developers by providing analysis tools and enabling them to use it in IDEs or editors that support LSP.

    Currently, it is possible to enable or disable it from Edit → Preferences → Qt Quick → QML/JS Editing → check/uncheck Turn On.

    Based on the documentation, QMLLS needs to know where your build directory is, and for this, it requires you to have a .qmlls.ini file in your source directory.

    The .qmlls.ini file can also be automatically generated by setting QT_QML_GENERATE_QMLLS_INI inside your CMake file (set(QT_QML_GENERATE_QMLLS_INI ON);) or by passing -DQT_QML_GENERATE_QMLLS_INI=ON to CMake.

    After this, and based on the OP's confirmation, the warnings should go away.


    At the time of writing this, I tried to update my Qt from 6.6 to 6.7 and then to 6.8, but QMLLS seems to be crashing with no output. Rerunning QMLLS in Qt Creator is possible from Tools → QML/JS → Run Checks, but when doing this (and having the correct build path set in .qmlls.ini), QMLLS crashes.

    Considering this, along with the notes in the Qt documentation and lots of bug reports, I can only suggest disabling this tool and waiting for a more stable version in the future.