- setStyleSheet基本語法:this->setStyleSheet("color: rgb(0, 255, 0); font-size:20px");
- 一些常用參數:
font: bold; 是否粗體顯示
font-size:20px; 來設定字體大小
font-weight:20px; 來設定字體深淺
color:black ;字體顏色
border-image:""; 用來設定邊框的背景圖片。
border-radius:5px; 用來設定邊框的弧度。可以設定圓角的按鈕
border-width: 1px; 邊框大小
設置背景顏色
background:transparent; 設置背景為透明
color:rgb(241, 70, 62); 設置前景顏色
selection-color:rgb(241, 70, 62); 用來設定選中時候的顏色
重要問題:
- setStyleSheet設置的樣式會被所有子類引用:
this->setStyleSheet("background-color: rgb(0, 255, 0)");
解決:限定模塊
this->setObjectName("w2"); this->setStyleSheet("#w2{background-color: rgb(0, 255, 0)}");
2.setStyleSheet設置背景圖片時,如果所在類沒有父類,既是第一層widget時,則不會顯示背景圖片,替代方案:
QPalette pal = this->palette(); pal.setBrush(QPalette::Background, QBrush(QPixmap("./pic/1.jpg"))); setPalette(pal);
3.相對路徑的問題:Windows用相對路徑時:"./pic/1.jpg"
查詢相對路徑所在文件夾:
#include <QDebug> #include<QDir> qDebug() << QDir::currentPath();