Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create certain QML types in a singleton

Tags:

qt5

qml

I have a QML singleton for use in styling defined as follows:

pragma Singleton
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1

QtObject {
    property ProgressBarStyle progressBarErrorStyle: ProgressBarStyle {
        background: Rectangle {
            radius: 2
            color: "lightgray"
            border.color: "gray"
            border.width: 1
            implicitWidth: 200
            implicitHeight: 20
        }
        progress: Rectangle {
            color: "orangered"
            border.color: "red"
        }
    }
}

I'm able to import the object and use it, however progressBarErrorStyle is always given the type ProgressBarStyle_QMLTYPE_17. If I change it to a Rectangle, then it is correctly typed as QQuickRectangle.

The QtQuick.Controls.Styles import defines ProgressBarStyle, and in QtCreator I'm not getting any syntax errors... so why is my object given the wrong type at runtime?

like image 881
Tim Avatar asked Oct 13 '25 11:10

Tim


1 Answers

You should use Component as the property type:

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2

Rectangle {
    property Component progressBarErrorStyle: ProgressBarStyle {
        background: Rectangle {
            radius: 2
            color: "lightgray"
            border.color: "gray"
            border.width: 1
            implicitWidth: 200
            implicitHeight: 20
        }
        progress: Rectangle {
            color: "orangered"
            border.color: "red"
        }
    }

    ProgressBar {
        id: progressBar

        NumberAnimation {
            target: progressBar
            property: "value"
            to: 1
            running: true
            duration: 2000
        }

        style: progressBarErrorStyle
    }
}

The components for styles are used in Loader items internally, which create instances of the components when they need to, just like delegates in Qt Quick's ListView, for example.

like image 105
Mitch Avatar answered Oct 16 '25 06:10

Mitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!