Set the general value of a property in QML, such as QSS

For example, I have two different QML elements with a common property, for example:

import QtQuick 2.0 Rectangle { width: 360 height: 360 Text { id: t color: "red" text: qsTr("Hello World") anchors.top: parent.top } TextInput { text: qsTr("Hello all!") color: "red" anchors.top: t.bottom } } 

You can see that Text and TextInput have an equal property called "color" with equal value.

In QSS, I can use the general value of a property, for example:

 QWidget { background: "red" } 

and all QWidgets belonging to the qss widget will also have a red background.

Is it possible to set a common property in QML?

+4
source share
1 answer

There is no support for configuring QSS in QML. But you can use the "Style Object" method to set properties and use them in all of your QML files.

In this, you define a Style object in the Style.qml file with properties that define the style. Create an instance in the root component so that it is available throughout the application.

 // Style.qml QtObject { property int textSize: 20 property color textColor: "green" } // root component Rectangle { ... Style { id: style } ... } // in use Text { font.pixelSize: style.textSize color: style.textColor text: "Hello World" } 

You can find more information here .

+8
source

All Articles