为什么不能为样式定义别名?例如
Button {
property alias color: theText.color
style: ButtonStyle {
label: Text {
id: theThext
}
}
}
给出一个
qml 无效别名引用无法找到 id
theText
为什么不能为样式定义别名?例如
Button {
property alias color: theText.color
style: ButtonStyle {
label: Text {
id: theThext
}
}
}
给出一个
qml 无效别名引用无法找到 id
theText
与此答案类似,这是因为alias
引用的项目是动态加载的。样式组件Label
就是这样:Component
s。它们是用于创建实际Item
加载的实际样式的模板Loader
。
这是我的解决方案(工作示例):
//MyCustomBtn.qml
import QtQuick 2.0
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
Button {
function setFontColor(fontColor){color_ = fontColor}
property string color_: "black"
style: ButtonStyle {
label: Text {
color: color_
text: control.text
}
}
}
//MyTest.qml
MyCustomBtn {
text: qsTr("Hello World")
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
Component.onCompleted: setFontColor("red")
}
使用默认字体大小
Button {
property int fontPixelSize: 0
style: ButtonStyle {
label: Text {
font.pixelSize: fontPixelSize ? fontPixelSize : font.pixelSize
text: control.text
}
}
我的解决方案是像文本一样获取控件的属性。您甚至可以将此样式保存到单独的样式文件并设置控件的 labelColor 属性。
Button {
property string labelColor: "yellow"
text: "hede"
style: ButtonStyle {
label: Text {
color: control.labelColor
text: control.text
}
}
}