Attached properties in QML

Can I create my own bound for the whole property as a component?

Item{
    Component.onCompleted : {} // Component is attached to everyone Items
}

      

+3


source to share


1 answer


You may not be able to attach properties to elements or components that you haven't created. But why do you want to do this anyway?

You might want to consider using signals and global properties instead.

For global properties that you can access from anywhere, you can set the context property of the context root declarative representation .. p>

ie,

QmlApplicationViewer viewer;
MyPropertyClass myProp;
viewer->rootContext()->setContextProperty("MyPropClass", &myProp);

      



Now in your QML file you can access the properties of this class as

Rectangle {
    Text {
        text: MyPropClass.getMyPropText()
    }
    MouseArea {
        onClicked: { MyPropClass.text = "Clicked" }
    }
}

      

This will call the getMyPropText () Q_INVOKABLE method from MyPropertyClass. and Q_PROPERTY "text" can be set when any signal is issued.

Will this suit you?

+2


source







All Articles