【问题标题】:How to get the background color of QT-toolButton/pushButton using C++ [duplicate]如何使用 C++ 获取 QT-toolButton/pushButton 的背景颜色 [重复]
【发布时间】:2016-04-30 15:02:33
【问题描述】:

相关问题:

解决问题的方法至少有3种:

// 1st 
QColor color = ui->toolButton->palette().color(QWidget::backgroundRole());
// 2nd
QColor color = ui->toolButton->palette().background().color();
// 3rd
QColor color = colorSetting = ui->toolButton->palette().color(QPalette::Window);

更新:对不起,我犯了一些错误,以下两种方法都可以正常工作。


原始问题:

我试过了

QColor color = ui->toolButton->palette().background().color();

QColor color = colorSetting = ui->toolButton->palette().color(QPalette::Window);

都得到了QColor(ARGB 1, 0.941176, 0.941176, 0.941176),不是我想要的正确颜色。

编辑mainwindow.ui设置背景色,更改toolButton的样式表 给background-color: rgb(255, 170, 255);

image of my toolButton

对于 pyQt,请参阅此处How to get the background color of a button or label (QPushButton, QLabel) in PyQt

【问题讨论】:

    标签: qt qtoolbutton


    【解决方案1】:

    您的链接问题关于按钮使用的颜色角色不正确。您正在寻找QPalette::Button 作为您的ColorRole

    QColor color = ui->toolbutton->palette()->color(QPalette::Button);
    

    是这种颜色可能不代表为工具按钮绘制的背景。有些样式使用渐变,QPalette 存储画笔,而不是颜色。调用QPalette::button()获取按钮背景画笔。

    我怀疑您打算更改背景颜色。您可以拨打setBrush()进行设置:

    //Create a solid brush of the desired color
    QBrush brush(someColor);
    //Get a copy of the current palette to modify
    QPalette pal = ui->toolbutton->palette();
    //Set all of the color roles but Disabled to our desired color
    pal.setBrush(QPalette::Normal, QPalette::Button, brush);
    pal.setBrush(QPalette::Inactive, QPalette::Button, brush);
    //And finally set the new palette
    ui->toolbutton->setPalette(pal);
    

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 1970-01-01
      • 2019-03-31
      • 2019-06-24
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多