Search code examples
cmakeqmlqtquick2qt-quickqt6

QML project gives 'unavailable type error' with CMake configuration (Qt 6.2)


I have created a test project with Qt6.2-MinGW-64bit and I choose CMake. The project structure is shown in the picture. Only 3 test files are embedded in each other and the first test file (test1.qml) is used in main.qml.

Project structure

Now when I want to start the program it gives a weird error that the Test2 is unavailable. I have tried different scenarios and have checked all steps in CMake but with no success.

Here is the main.qml:

import QtQuick
import untitled35

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    property Test1 _example: Test1 {}
}

Here is the CMakeFile:

cmake_minimum_required(VERSION 3.16)

project(untitled35 VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.2 COMPONENTS Quick REQUIRED)

qt_add_executable(appuntitled35
    main.cpp
)

qt_add_qml_module(appuntitled35
    URI "untitled35"
    VERSION 1.0
    QML_FILES
        main.qml
        core/Test1.qml
        core/Test2.qml
        core/components/Test3.qml
)

set_target_properties(appuntitled35 PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_compile_definitions(appuntitled35
    PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(appuntitled35
    PRIVATE Qt6::Quick)

Solution

  • Your issue is that Test2 has an error. QtObject does not have a default property to put the Test3 object you created inside of it. Item places child elements in its children, but QtObject does not make that assumption. try changing the QtObject to Item. Documentation for default properties and Documentation for Item data property which is its default.