Łukasz Przeniosło
Łukasz Przeniosło

Reputation: 2949

Declare specific global objects in QML

I am trying to follow this answer, but for more complicated objects:

https://stackoverflow.com/a/25123824/1036082

My files are:

qml.qrc:

<RCC>
    <qresource prefix="/">
        <file>main.qml</file>
        <file>Uconsts.qml</file>
        <file>Ucolors.qml</file>
        <file>qmldir</file>
    </qresource>
</RCC>

main.qml:

import QtQuick 2.9
import QtQuick.Window 2.2
import "."

Window
{
    width: 600
    height: 400
    visible: true
    color: Uconsts.colDay.canvas;

}

qmldir:

# qmldir
singleton Uconsts Uconsts.qml

Ucolors.qml:

import QtQuick 2.0

Item
{
    property color canvas: "#FFFFFF";
}

Uconsts.qml:

pragma Singleton
import QtQuick 2.9

QtObject
{
    property Ucolors colDay:
    {
        canvas: "#eaedf1";
    }
}

When running the program, in runtime I get the following errors from the QML side:

qrc:/main.qml:10: TypeError: Cannot read property 'canvas' of null
qrc:/Uconsts.qml:6:2: Unable to assign QString to Ucolors_QMLTYPE_0*

What am I doing wrong here?

Upvotes: 1

Views: 720

Answers (1)

eyllanesc
eyllanesc

Reputation: 244182

QML does not deduct the type when you use {}, you must create the item explicitly

pragma Singleton
import QtQuick 2.9

QtObject
{
    property Ucolors colDay: Ucolors{ canvas: "#eaedf1";}
}

Upvotes: 1

Related Questions