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 {
}
}
.qmlls.inifile, which should be in your project directory. This file can be auto-generated by addingset(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.