Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Failed to import C++ class into QML in Qt6.5
Forum Updated to NodeBB v4.3 + New Features

Failed to import C++ class into QML in Qt6.5

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 199 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tomek19991999
    wrote on last edited by
    #1

    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:

    1. Create a new Qt Quick Application project with Qt 6.5.
    2. Use the C++ class wizard to create a new class:
    • Base class: QObject.
    • Check the option to add QML_ELEMENT.
    1. 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!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tomek19991999
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T tomek19991999 has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved