0

相关问题:

解决问题的方法至少有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);

我的工具按钮的图像

对于 pyQt,请参见此处如何在 PyQt 中获取按钮或标签(QPushButton、QLabel)的背景颜色

4

1 回答 1

0

您的链接问题关于按钮使用的颜色角色不正确。您正在寻找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);
于 2016-04-30T16:21:21.563 回答