syn*_*ker 4 qt qml qt-quick qtquick2
例如,我有2个不同的QML元素具有共同属性,例如:
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
}
}
Run Code Online (Sandbox Code Playgroud)
您可以看到,Text和TextInput具有相等的属性,称为"color",具有相等的值.
在QSS中,我可以使用公共属性值,例如:
QWidget {
background: "red"
}
Run Code Online (Sandbox Code Playgroud)
和属于qss小部件的所有QWidgets也将具有红色背景.
在QML中设置公共属性的方法是什么?
在QML中不支持使用QSS进行自定义.但您可以使用"样式对象"方法设置属性并在所有QML文件中使用它们.
在此,您可以在"Style.qml"文件中定义Style对象,并使用定义样式的属性.在根组件中实例化,因此它将在整个应用程序中可用.
// 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"
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到更多信息.
| 归档时间: |
|
| 查看次数: |
3006 次 |
| 最近记录: |