Failed to import C++ class into QML in Qt6.5
Solved
QML and Qt Quick
-
Hello,
I'm trying to create the simplest possible example of importing a C++ class into QML, but I encounter the following error:
"Failed to import cpp_class_to_qml_import. Are your import paths set up properly? [import]"Steps to reproduce the problem in Qt Creator:
- Create a new Qt Quick Application project with Qt 6.5.
- Use the C++ class wizard to create a new class:
- Base class: QObject.
- Check the option to add QML_ELEMENT.
- Make import in main.qml: import Test_import_app
CMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(Test_import_app VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(appTest_import_app main.cpp ) qt_add_qml_module(appTest_import_app URI Test_import_app VERSION 1.0 QML_FILES Main.qml SOURCES testclass.h testclass.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(appTest_import_app PROPERTIES # MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appTest_import_app 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(appTest_import_app PRIVATE Qt6::Quick ) include(GNUInstallDirs) install(TARGETS appTest_import_app BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
testclass.h:
#ifndef TESTCLASS_H #define TESTCLASS_H #include <QObject> #include <QQmlEngine> class TestClass : public QObject { Q_OBJECT QML_ELEMENT public: explicit TestClass(QObject *parent = nullptr); signals: }; #endif // TESTCLASS_H
Main.qml:
import QtQuick import Test_import_app Window { width: 640 height: 480 visible: true title: qsTr("Hello World") }
Can anyone help me understand what might be missing or configured incorrectly? Thank you in advance!
-
Solution:
Everything was working correctly except for the import warning in Qt Creator.
The issue is related to Qt Creator itself and not the Qt framework. The application runs and works as expected without any issues.
This import warning can be safely ignored, as it does not affect the functionality of the application. -