【问题标题】:Creating a custom QML Button with native look and feel创建具有本机外观的自定义 QML 按钮
【发布时间】:2015-12-29 21:00:44
【问题描述】:

我想通过定义我自己的 QML 类型来创建自定义 Button。这个Button 类型应该包含两个文本字段,一个是符号字体的单个字符,另一个是实际的按钮文本。

这很简单,但是如何使用为目标系统定义的本机颜色、渐变、字体和边框?

是否可以扩展Button 本身?以及如何在扩展 Button 时禁用设置图像的可能性?

import QtQuick 2.5

Rectangle {
    id:anyButton

    property string image:"\ue43f"
    property string text:"Button"

    border.color : "black"
    border.width: 1
    radius: 5

    Gradient {
        id: lightGradient
        GradientStop { position: 0.0; color: anyButton.color }
        GradientStop { position: 1.0; color: Qt.darker(anyButton.color,1.5) }
    }

    Gradient {
        id: darkGradient
        GradientStop { position: 0.0; color: Qt.darker(anyButton.color,1.7) }
        GradientStop { position: 1.0; color: Qt.darker(anyButton.color,1.7) }
    }

    Rectangle{
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter

        Text{
            id:buttonImage
        }
        Text{
            id: buttonLabel
            font.pixelSize:20
            text: anyButton.text
        }
    }

    signal buttonClick()

    MouseArea{
        id: buttonMouseArea
        anchors.fill: parent
        onClicked: parent.buttonClick()
        hoverEnabled: true

        onEntered:{
            parent.border.width= 2
        }

        onCanceled:{
            parent.border.width= 1
        }

        onExited: {
            parent.border.width= 1
        }
    }
    gradient: buttonMouseArea.pressed ? darkGradient : lightGradient
}

【问题讨论】:

    标签: qt qml qtquick2 qt5.5


    【解决方案1】:

    在 QML 中创建具有自定义样式、文本等的按钮非常简单。 使用ButtonStyle,您可以根据需要定义自定义backgroundlabel。要获取系统颜色,请使用SystemPaletteHere你可以找到它的真实控件应用。

    例如:

    Button {
        anchors.centerIn: parent
        property string firstfield: "a"
        property string secondfield: "sometext"
        iconSource: "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"
        text: firstfield + " " + secondfield
        style: ButtonStyle {
            background: Rectangle {
                id: bg
                border.width: 1
                border.color: palette.mid
                radius: 3
                gradient: Gradient {
                    GradientStop { position: 0.0; color: control.pressed ? palette.button : palette.light }
                    GradientStop { position: 0.5; color: palette.midlight }
                    GradientStop { position: 1.0; color: control.pressed ? palette.light : palette.button }
                }
            }
            label: RowLayout {
                id: row
                spacing: 5
                Image { source: control.iconSource }
                Label {text: control.firstfield; font.family: "Symbol"; font.pixelSize: 18; color: palette.buttonText}
                Label {text: control.secondfield; color: palette.buttonText}
    
            }
        }
    }
    
    SystemPalette { id: palette; colorGroup: SystemPalette.Active }
    

    当然,您可以添加阴影等。如果您放弃ButtonStyle,它应该看起来像一个常规按钮

    【讨论】:

    • 谢谢,但我怎样才能阻止用户设置按钮图标,以及如何保持设置自定义标签布局的可能性。你的例子并没有真正解决问题。我正在寻找更通用的解决方案。
    • @Avedo 我认为无论你做什么都是毫无价值的。考虑这个简单的例子:我没有修改你的按钮,而是在它上面放置一个Image,而不是在你的按钮内。然后我将它的state 绑定到你的按钮state:就是这样,我已经使用你的按钮模仿了一个带有图像的按钮。组件分层随用随用,不可控制。
    • 我认为你应该澄清你的问题。您想要实现的目标并不完全清楚
    猜你喜欢
    • 2012-12-19
    • 1970-01-01
    • 2014-02-15
    • 2016-09-25
    • 2017-05-02
    • 1970-01-01
    • 2022-01-23
    • 2013-08-22
    • 2012-04-15
    相关资源
    最近更新 更多