【问题标题】:Change radio button text color in Qt更改 Qt 中的单选按钮文本颜色
【发布时间】:2015-04-29 14:16:11
【问题描述】:

我尝试过样式表、html 格式和调色板来将单选按钮的颜色更改为白色,但它们不起作用。

有没有办法改变它? QRadioButton的文档中没有文字颜色的功能。

【问题讨论】:

    标签: qt qt-creator qt5 qtgui


    【解决方案1】:

    如果您只想更改单选按钮的前景色,您可能需要为该按钮提供 alpha 背景色。例如:

    QRadioButton{ color: rgb(255, 255, 255); background-color: rgba(255, 255, 255, 0);}
    

    这将为您提供具有透明背景的白色。

    【讨论】:

      【解决方案2】:

      这听起来很奇怪。 QtCreator 和 QtDesigner 都将 QRadioButton 的 stylesheet 属性设置为

      color: white; background-color: red;
      

      给你一个红底白字的 QRadioButton (如果我理解这个问题)

      【讨论】:

      • 我试了很多次,文字颜色没有变化。
      • 您能否发布一个代码示例(如果您尝试使用代码执行此操作),说明您在尝试什么?
      • 我重新安装了 qt 编译器,问题解决了:/太傻了,我不知道为什么会这样:(
      【解决方案3】:

      您将需要继承 QProxyStyle 并重新实现 drawControl() 方法以捕获带有 QStyle::CE_RadioButton 的调用。

      如果您查看QRadioButton::paintEvent() 的来源:

      QStylePainter p(this);
      QStyleOptionButton opt;
      initStyleOption(&opt);
      p.drawControl(QStyle::CE_RadioButton, opt);
      

      initStyleOption() 除了设置状态数据什么都不做;一切都由画家处理。这是QStylePainter::drawControl(),它只是调用当前的QStyle 来完成这项工作:

      void QStylePainter::drawControl(QStyle::ControlElement ce, const QStyleOption &opt)
      {
          wstyle->drawControl(ce, &opt, this, widget);
      }
      

      Qt 文档包含有关如何子类化和加载代理样式的信息:http://doc.qt.io/qt-5/qproxystyle.html。查看 QCommonStyle::drawControl() 实现,了解 Qt 如何绘制控件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-21
        • 2021-11-20
        相关资源
        最近更新 更多