Custom properties don't exist at compile time? "Cannot assign to non-existent property"

by DFaller   Last Updated August 14, 2019 00:26 AM

In referencing an item's properties from outside its parent component, I can get a "Cannot assign to non-existent property" error that seems to depend on some compile time order-of-operations.

I have created a small example app that shows some various ways to assign a color to this property, where direct assignment fails, but similar assignment to default properties works, or even later assignment works.

Here is my main.qml:

import QtQuick 2.7
import QtQuick.Window 2.12

Window {
    id: application_window
    visible: true
    width: 640
    height: 480

    Thing {
//        colors.backgroundColor: "red"  // Direct assignment of custom property: Fails
//        thing.color: "red"           // Direct assignment of default property: Works
//        Component.onCompleted: colors.backgroundColor = "red"  // Run time assignment of custom property: Works

and a file called Thing.qml in the same dir:

Item {
    id: root
    height: 50
    width: 50
    property alias colors: colors
    property alias thing: thing

    Rectangle {
        id: thing
        anchors.fill: root
        color: colors.backgroundColor

    Item {
        id: colors
        property color backgroundColor: "blue"

By individually uncommenting the lines in main.qml, you can see that directly assigning colors.backgroundColor does not work, but the other ways of changing the color do work, even assigning colors.backgroundColor at runtime. I have also moved the 'colors' Item to a different file, which allows direct assignment (I guess the backgroundColor becomes considered like a default property in this case). Is there any way to directly assign the colors.background color without a separate file or waiting until runtime?

Tags : qml

Related Questions