Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top