5

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 {

    }
}
11
  • Does this help? stackoverflow.com/a/78816252/11603485 Commented Oct 11, 2024 at 18:40
  • @smr Unfortunately not, the problem persists even after cleaning and rebuilding Commented Oct 11, 2024 at 19:40
  • Did you try cleaning/rebuilding and using Reset Code Model (from Tools → QML/JS → Reset Code Model)? What I can confirm is that there are no warnings on Windows/QC 14.0.2/Qt 6.6.1. Commented Oct 14, 2024 at 18:18
  • Reset code model doesn't seem to change anything. If I create a new project in QtCreator; then add a c++ class with QML_ELEMENT and Q_OBJECT; then import the module and use the class in Main.qml I consistently get the warnings. I'm using Qt Creator 14.0.2, based on Qt 6.7.3 on Windows 10 if that helps. Commented Oct 14, 2024 at 19:45
  • 1
    So I was able to successfully reproduce the same warnings, and as far as I can tell, it's a bug. The issue comes from QMLLS, the new language server for QML, which cannot find your C++ modules. One thing you are currently missing is a .qmlls.ini file, which should be in your project directory. This file can be auto-generated by adding set(QT_QML_GENERATE_QMLLS_INI ON) in your CMake file. If you just want to suppress the warnings, you can disable the language server by going to Edit → Preferences → Qt Quick → QML/JS Editing → uncheck Turn On. Commented Oct 16, 2024 at 20:00

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

The warning is accurate.

The import cannot find module CppIntegrationTest, because that's the name of the CMake project.

The module you're generating is called appCppIntegrationTest. The easiest way to fix this is to change the module name into CppIntegrationTest too (there will be no conflict but you can also rename the CMake project name if you like)

2 Comments

So why can the project run? I successfully reproduced the same errors, and for now, it seems like some sort of bug to me.
I converted active and large qt5 qmake-based codebases that use qtcreator resources to cmake based builds. In my experience there is a lot of half-baked cleverness in the qt_xxx macro's that will often do what you want, sometimes even more, or just not without any scrap of info. The documentation is sketchy and the communication from the tools doesn't always reflect what is happening. I'd hope qt6 improved on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.