同時設置按鈕背景色與字體顏色
端午節的最後一天沒事幹跑來公司加班哈哈,纔想起之前記錄的筆記還沒發博客永久記錄一下,趕緊寫了下來
在工作過程中,需要用到同時設置按鈕背景色與字體顏色,百度了很多資料,網上所說的大部分都說
login_button->setStyleSheet(“background-color: rgb(0, 0, 255)”);
login_button->setStyleSheet(“color: rgb(255, 255, 255)”);
但是這樣子根本就實現不了,只實現了
login_button->setStyleSheet(“color: rgb(255, 255, 255)”);
後者而已,也就是這個字體設置,background-color其實被color覆蓋了,所以沒辦法這樣去設置,後面我又自己摸索了,終於弄出來了,特地記錄下來:
QPalette pe1;
pe1.setColor(QPalette::ButtonText,Qt::white);
login_button->setText("登錄");
login_button->setPalette(pe1);
login_button->setStyleSheet("background-color: rgb(0, 0, 255)");
也就是不要都是用setStyleSheet去設置,可以一個是去setPalette,另一個是setStyleSheet。