14

我是 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"
        }*/
}
4

5 回答 5

19

根据文档

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
    }
}
于 2017-06-01T06:46:31.603 回答
10

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

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";
     }
}
于 2020-09-29T21:55:03.077 回答
5

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

Button
{
    //...
    text: "<font color='#fefefe'>" + moudle + "</font>"
    font.family: "Arial"
    font.pointSize: 24
    //...
}
于 2019-01-07T05:50:11.123 回答
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 <QQuickStyle>QQuickStyle::setStyle("Material");到 main.cpp

于 2019-06-07T13:23:07.103 回答
0
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"
    }
}

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

于 2021-11-11T08:25:32.283 回答