2016-04-30 41 views
0

相關Qusetions得到QT-工具按鈕/按鍵的背景色:如何使用C++

至少有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); 

更新:抱歉,我犯了一些錯誤,以下兩種方式工作。


原Qusetion:

我已經試過

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,工具按鈕 的變化樣式表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

回答

0

你鏈接的問題是不正確有關使用的顏色角色按鈕。您正在尋找QPalette::Button作爲您的ColorRole

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

BE的這種顏色可能不代表畫的工具按鈕背景。一些款式使用漸變和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);