2

为什么不能为样式定义别名?例如

Button {

    property alias color: theText.color

    style: ButtonStyle {
        label: Text {
            id: theThext  
        }
    }
}

给出一个

qml 无效别名引用无法找到 idtheText

4

4 回答 4

3

与此答案类似,这是因为alias引用的项目是动态加载的。样式组件Label就是这样:Components。它们是用于创建实际Item加载的实际样式的模板Loader

于 2014-04-30T20:58:36.737 回答
1

这是我的解决方案(工作示例):

//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")
}
于 2015-02-20T12:11:58.607 回答
0

使用默认字体大小

Button {
    property int fontPixelSize: 0
    style: ButtonStyle {
        label: Text {
            font.pixelSize: fontPixelSize ? fontPixelSize : font.pixelSize
            text: control.text
        }
    }
于 2015-08-08T09:47:59.013 回答
0

我的解决方案是像文本一样获取控件的属性。您甚至可以将此样式保存到单独的样式文件并设置控件的 labelColor 属性。

Button {
    property string labelColor: "yellow"
    text: "hede"
    style: ButtonStyle {
        label: Text {
            color: control.labelColor
            text: control.text
        }
    }
}
于 2019-04-15T12:37:54.970 回答