更改 QML 的 RadioButton 中文本的颜色

Posted

技术标签:

【中文标题】更改 QML 的 RadioButton 中文本的颜色【英文标题】:change color of text in a RadioButton of QML 【发布时间】: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 的控件可以解决问题。 【参考方案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 有这样的东西吗?

以上是关于更改 QML 的 RadioButton 中文本的颜色的主要内容,如果未能解决你的问题,请参考以下文章

使用QML怎么实现radiobutton?还有,显示时,将字体放在radiobutton前面

求教QML RadioButton单选问题

如何在 Cascades、Blackberry 10 中使用 Qt/QML/C++ 从另一个 qml 文件中更改一个 qml 文件中的标签文本?

qml中仅一个单词的文本颜色更改

如何捕获 QML 中更改值的 Spinbox 事件?

在 C++ 中更改 QML font.pointSize