c++ - Cannot define Object Types through QML Documents - Stack Overflow

admin2025-04-29  3

I have spent days researching and trying to figure this out, it should just work - but does not.

I am attempting to create and use a simple QML object as a single source of setting for my project.

As a test I have created a new Qt 6.7.3 QML project, and I added a new .qml file "TestQmlObj.qml" with a few properties.

Intellisense works, but I consistently get Unable to assign [undefined] to errors when run.

TestQmlObj.qml

pragma Singleton
import QtQuick

QtObject {

    readonly property color primaryColor: "red"
    readonly property color secondaryColor: "#FFC107"
    readonly property color backgroundColor: "#FFFFFF"

    readonly property font mainfont: Qt.font({
        family: "Arial",
        pointSize: 12,
        weight: Font.Normal
    })

    readonly property font headerFont: Qt.font({
        family: "Arial",
        pointSize: 16,
        weight: Font.Bold
    })
}

I then tried to use these properties in the Main.qml file.

Main.qml

import QtQuick

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

    Rectangle {
        anchors.fill: parent
        //color: "red"
        color: TestQmlObj.primaryColor
        //color: TestQmlObj.backgroundColor

        Text {
            text: qsTr("text")
            font: TestQmlObj.headerFont
        }
    }
}

I have tried all kinds of suggested changes but in all cases I receive the following error and the color does not change.

qrc:/qt/qml/QmlObjTypes/Main.qml:13:9: Unable to assign [undefined] to QColor

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(QmlObjTypes 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(appQmlObjTypes
    main.cpp
)

qt_add_qml_module(appQmlObjTypes
    URI QmlObjTypes
    VERSION 1.0
    QML_FILES
        Main.qml
        TestQmlObj.qml
)

# 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(appQmlObjTypes PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appQmlObjTypes
    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(appQmlObjTypes
    PRIVATE Qt6::Quick
)

include(GNUInstallDirs)
install(TARGETS appQmlObjTypes
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

I have spent days researching and trying to figure this out, it should just work - but does not.

I am attempting to create and use a simple QML object as a single source of setting for my project.

As a test I have created a new Qt 6.7.3 QML project, and I added a new .qml file "TestQmlObj.qml" with a few properties.

Intellisense works, but I consistently get Unable to assign [undefined] to errors when run.

TestQmlObj.qml

pragma Singleton
import QtQuick

QtObject {

    readonly property color primaryColor: "red"
    readonly property color secondaryColor: "#FFC107"
    readonly property color backgroundColor: "#FFFFFF"

    readonly property font mainfont: Qt.font({
        family: "Arial",
        pointSize: 12,
        weight: Font.Normal
    })

    readonly property font headerFont: Qt.font({
        family: "Arial",
        pointSize: 16,
        weight: Font.Bold
    })
}

I then tried to use these properties in the Main.qml file.

Main.qml

import QtQuick

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

    Rectangle {
        anchors.fill: parent
        //color: "red"
        color: TestQmlObj.primaryColor
        //color: TestQmlObj.backgroundColor

        Text {
            text: qsTr("text")
            font: TestQmlObj.headerFont
        }
    }
}

I have tried all kinds of suggested changes but in all cases I receive the following error and the color does not change.

qrc:/qt/qml/QmlObjTypes/Main.qml:13:9: Unable to assign [undefined] to QColor

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(QmlObjTypes 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(appQmlObjTypes
    main.cpp
)

qt_add_qml_module(appQmlObjTypes
    URI QmlObjTypes
    VERSION 1.0
    QML_FILES
        Main.qml
        TestQmlObj.qml
)

# 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(appQmlObjTypes PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appQmlObjTypes
    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(appQmlObjTypes
    PRIVATE Qt6::Quick
)

include(GNUInstallDirs)
install(TARGETS appQmlObjTypes
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
Share Improve this question edited Jan 9 at 5:49 musicamante 48.9k8 gold badges41 silver badges74 bronze badges asked Jan 8 at 12:54 user1771706user1771706 2393 silver badges10 bronze badges 3
  • 1 If you want your TestQmlObj to be a singleton, you need to specify that in your CMake file: set_source_files_property(TestQmlObj.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE) – JarMan Commented Jan 8 at 13:56
  • Thank you JarMan, that worked. I have 2 follow up questions: 1) How would I do this when not creating a singleton? When I comment out the pragma Singleton the same errors return. 2) I'm new to QML and searched for days for a solution. How should I have searched to find set_source_files_properties when all I had was the "Unable to assign [undefined] to" error? – user1771706 Commented Jan 8 at 14:21
  • It's not something you can understand only from the error message. If you hadn't provided the source code, then no one would know what the problem is. The error simply indicates that TestQmlObj.primaryColor is undefined. And by examining the code, you should realize that TestQmlObj should be a singleton or an attached property, – smr Commented Jan 8 at 16:22
Add a comment  | 

1 Answer 1

Reset to default 2

The reason you are unable to access TestQmlObj is because you are attempting to use it as a singleton, but you have not finished declaring it as such. The docs describe two methods for declaring singletons:

  1. If you define your singleton from QML, you must label it with pragma Singleton AND you must add an entry into the qmldir file of your module. The best way to do that is by setting the source property in your CMakeLists.txt file.
set_source_files_property(TestQmlObj.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
  1. The other way is to define your singleton object from C++ using the QML_SINGLETON macro.
class MySingleton : public QObject
{
    Q_OBJECT
    QML_SINGLETON
    QML_ELEMENT
public:
    MySingleton(QObject *parent = nullptr) : QObject(parent) {
        // ...
    }
};

Since you have defined your object in QML, you should use option 1 and make sure you update your CMakeLists.txt file.

转载请注明原文地址:http://anycun.com/QandA/1745857215a91297.html