Qt中使用setStyleSheet對QPushButton按鈕進行外觀設置


Qt中使用setStyleSheet對按鈕進行外觀設置

字體顏色的設置一般時以下兩種方案:

(1)屬於QWidget子類的一些控件

可以直接使用樣式表,例如label->setStyleSheet("color:white");
(2)不屬於QWidget子類的控件

可以考慮設置其前景色,例如各種ViewtreeWidgetItem->setForeground(0,QBrush(QColor(Qt::white)));

-------------------------------------------------------------------------------------------------

要實現的效果

正常狀態下:黑底(背景色),白字(前景色),圓角,向外凸起;

鼠標停留:背景和前景都反色;

鼠標按下:背景色變為淡藍色,向內凹陷。

代碼:

ui->pushButton_GoToProcess->setStyleSheet("QPushButton{background-color:black;\

                                            color: white;   border-radius: 10px;  border: 2px groove gray;\

                                            border-style: outset;}"

                                           "QPushButton:hover{background-color:white; color: black;}"

                                          "QPushButton:pressed{background-color:rgb(85, 170, 255);\

                                                           border-style: inset; }"

                                           );

結果:

(1)正常狀態

(2)鼠標停留


(3)鼠標按下


-------------------------------------------------------------------------------------------------

如果要對多個按鈕實現同樣的效果,只要將setStyleSheet里的字符串定義成QString,后面其他按鈕直接調用這個QString就好了。

定義:

QString button_style="QPushButton{background-color:black;\

                                      color: white;   border-radius: 10px;  border: 2px groove gray;\

                                      border-style: outset;}"

                                     "QPushButton:hover{background-color:white; color: black;}"

                                    "QPushButton:pressed{background-color:rgb(85, 170, 255);\

                                                     border-style: inset; }";

調用:

ui->pushButton_Save->setStyleSheet(button_style);



-------------------------------------------------------------------------------------------------

對於按鈕如果想設置為圖片,則最好使用添加icon,這樣的話位置比較合適,如果是使用setStyleSheet還需要對位置進行設置(直接設置會發現位置有點偏)。另外要加上一句:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM