2016-08-02 89 views
2

我試圖改變單選按鈕的顏色上選擇和取消爲 QtStylesheet:Qt StylesheetQRadioButton顏色變化選擇和取消的Qt

但在這個環節它僅指加載圖像,但我怎麼能改顏色和不加載圖像和改變邊界的顏色或樣式的單選按鈕

要求安裝在圖像:RadioButtonRequiredImage

感謝支持

感謝

普利文

+0

請仔細閱讀您鏈接的文檔。它指出'QRadioButton'支持[框模型](http://doc.qt.io/qt-4.8/stylesheet-customizing.html#box-model)(它有邊框)。它也有'background-color'屬性。 – ilotXXI

+0

是的,你是正確的背景顏色改變框的顏色,但我無法設置按鈕(o)顏色紅色(在選擇)和黑色(取消選擇) –

回答

-1

如果你想時,他的選擇,你應該同時使用兩個插槽和樣式改變你的單選按鈕的背景顏色。

我將命名您的按鈕MyButton。

在你的.h,你會發現:

private : 
     QRadioButton MyButton; 
    private slots: 
     void changeColorMyButton(); 

,並在您的.cpp加入你的主窗口的設置:

QObject::connect(MyButton,SIGNAL(clicked(bool)),this,SLOT(changeColorMyButton)); 

你的按鈕現在連接到信號點擊當你點擊你的按鈕時,插槽changeColorMyButton將被執行。您現在可以自定義您的插槽。

void Mainwindow::changeColorMyButton() 
    { 
     if(this.MyButton.isChecked()) 
     { 
     this.MyButton->setStyleSheet("background-color: black"); 
     } 
     else 
     { 
     this.MyButton->setStyleSheet("background-color: yellow"); 
     } 
    } 
2

仔細閱讀documentation。它描述了你所需要的一切。它甚至幾乎described你的情況,唯一的區別是圖像,而不是顏色。

爲你的情況樣式表是這樣的:

QRadioButton { 
    background-color:  gray; 
    color:     white; 
} 

QRadioButton::indicator { 
    width:     10px; 
    height:     10px; 
    border-radius:   7px; 
} 

QRadioButton::indicator:checked { 
    background-color:  red; 
    border:     2px solid white; 
} 

QRadioButton::indicator:unchecked { 
    background-color:  black; 
    border:     2px solid white; 
} 
+0

哦,它在第一次去工作謝謝!!!!!!! !!!!!!!!! :) –

0

設置樣式表旁邊的作品對我來說:

QRadioButton:checked{ 
    background-color: red; 
} 

QRadioButton:unchecked{ 
    background-color: black; 
} 

設置樣式表來QRadioButton ::指標:檢查不工作,因爲這隻會改變指標的設置。