【问题标题】:QML button change text colorQML按钮更改文本颜色
【发布时间】:2017-04-10 22:44:33
【问题描述】:

我是 QML 的新手,我想个性化我的按钮。我成功改变了背景颜色和边框颜色。但我根本没有成功改变按钮文本的颜色。我看到我们不再使用“样式”来更改样式,而是使用“背景”,我不了解它的所有内容。

感谢您的帮助。

Button {
        id: buttonAC
        text: qsTr("AC")
        Layout.fillHeight: true
        Layout.fillWidth: true

        background: Rectangle {
            border.color: "#14191D"
            color: "#24292f"
            // I want to change text color next
        }

        /*Text {
            text: qsTr("AC")
            color: "#F54035"
        }*/
}

【问题讨论】:

  • 您只需打开the documentationthis
  • 谢谢您,它解决了我的问题,我多次阅读此文档,但没有检索到好的信息。但是文档里写的很清楚。

标签: qt qml qtquick2 qtquickcontrols2


【解决方案1】:

根据doc

import QtQuick 2.6
import QtQuick.Controls 2.1

Button {
    id: control
    text: qsTr("Button")

    contentItem: Text {
        text: control.text
        font: control.font
        opacity: enabled ? 1.0 : 0.3
        color: control.down ? "#17a81a" : "#21be2b"
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        elide: Text.ElideRight
    }

    background: Rectangle {
        implicitWidth: 100
        implicitHeight: 40
        opacity: enabled ? 1 : 0.3
        border.color: control.down ? "#17a81a" : "#21be2b"
        border.width: 1
        radius: 2
    }
}

【讨论】:

    【解决方案2】:

    我发现的两种最快的方法是使用以下未记录的属性:

    Button {
         ....
         palette.buttonText: "white"
    }
    

    在用户交互期间自定义文本颜色时更进一步,这里是 Button 源代码中的三元组,后跟要相应设置的属性列表:

    color: control.checked || control.highlighted ? control.palette.brightText :
               control.flat && !control.down ? (control.visualFocus ? control.palette.highlight : control.palette.windowText) : control.palette.buttonText
    

    属性:

    control.palette.brightText
    control.palette.highlight
    control.palette.windowText
    control.palette.buttonText
    

    第二个肮脏的技巧是使用 onCompleted 插槽,如下所示:

    Button {
         id: control
         ....
         Component.onCompleted: {
              control.contentItem.color = "white";
         }
    }
    

    【讨论】:

      【解决方案3】:

      如果你只是想改变你的文本颜色,你可以在Button 中使用 html 字体样式会更好。这将保留其他Item 之类的按钮图标:

      Button
      {
          //...
          text: "<font color='#fefefe'>" + moudle + "</font>"
          font.family: "Arial"
          font.pointSize: 24
          //...
      }
      

      【讨论】:

      • 太棒了!你很酷 !在过去的 15 分钟里,我一直在努力设置 ToggleButton/ToggleButtonStyle 的颜色,您的方法有效:) 谢谢
      【解决方案4】:

      如果您使用 QML 样式,还有另一种方法。将 2.12 替换为您的 QML 版本。

      import QtQuick.Controls.Material 2.12    
      
      Button {
          id: goToParenFolder
          text: "Hi"
          flat: true
          Material.foreground: "red"
      }
      

      此按钮的文本将显示为红色,其他按钮将采用 Material Style 着色。

      要启用 QML 样式并添加 Material 主题,请将 QT += quickcontrols2 添加到您的 .pro 文件中。

      还将#include &lt;QQuickStyle&gt;QQuickStyle::setStyle("Material"); 添加到main.cpp

      【讨论】:

        【解决方案5】:
        Button {
            id: control
            width: 290; height: 80
            opacity: down ? 0.6 : 1
            background: Rectangle {
                color: "#4DABFB"
                radius: 50
            }
            Text {
                id: controlText
                anchors.fill: parent
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                font.pixelSize: 30
                color: "#FFFFFF"
                text: "OK"
            }
        }
        

        我喜欢在Button中使用Text,然后你可以随意改变Text的颜色。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-14
          • 1970-01-01
          • 1970-01-01
          • 2021-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多