3

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?

Tim
  • 4,560
  • 2
  • 40
  • 64
  • Can you please post the full example, with a main .qml file that shows how you're using this? Also, what type do you expect it to be? – Mitch Jun 04 '14 at 17:43
  • 1
    The question you give seems irrelevant with title (like in XY problem). Anyway, `ProgressBarStyle_QMLTYPE_17` is correct type, you could check `metaObject()->superClass()` to see that qml make another type. If you add any property to some object, qml will define a new metaobject, appending `_QMLTYPE_%d` or `_QML_%d`(qml file) sufix to its name. But unless you explicitly check it in C++, this should not a make a problem in your case. – Arpegius Jun 04 '14 at 19:30

1 Answers1

4

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.

Mitch
  • 23,716
  • 9
  • 83
  • 122