【问题标题】:change color of text in a RadioButton of QML更改 QML 的 RadioButton 中文本的颜色
【发布时间】:2019-05-13 11:30:31
【问题描述】:

使用Qt Quick Controls 1,有没有办法改变QML中RadioButton的文本颜色?

我想把它改成白色,因为我的背景是黑色的,找不到办法。

                RowLayout {
                    RadioButton {
                        width:15
                        height:15
                        text: "xlsx"
                        checked: true
                    }
                    RadioButton {
                        width:15
                        height:15
                        text: "Bottom"
                    }
                }

【问题讨论】:

  • 以下 2 个答案不是您想要的吗?创建后是否要动态设置颜色?请解释
  • 您好@bardao 不,我希望它在创建时进行自定义。也许版本 2 的控件可以解决问题。

标签: qt qml


【解决方案1】:

对于 Qt Quick Controls 2,您应该使用自定义 contentItem 创建自己的 RadioButton 组件:

RadioButton {
    id: control
    width: 15
    height: 15
    text: "xlsx"
    contentItem: Text {
        text: control.text
        color: "white"
        leftPadding: control.indicator.width + control.spacing
        verticalAlignment: Text.AlignVCenter
    }
}

查看文档中的完整示例:Customizing RadioButton

对于 Qt Quick Controls 1,请参阅@Roya Ghasemzadeh 的答案。

【讨论】:

  • 这可能是我正在寻找的解决方案。
【解决方案2】:

您需要为您的单选按钮定义一个样式。例如,对于第二个 radioButton,您可以拥有:

    RadioButton 
        {
            width:15
            height:15


            style: RadioButtonStyle
            {
                label: Text
                {
                    color: "white";
                    text: "Bottom"

                }                                   
            }
        }

【讨论】:

    【解决方案3】:

    试试这个:

    radioButton->setStyleSheet("QRadioButton{ background-color : black; color : white; }");

    【讨论】:

    • qml 有这样的东西吗?
    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多