QT Style Sheets是一個很有利的工具,允許定製窗口的外觀,
此外還可以用子類QStyle來完成,他的語法很大比重來源於html的CSS,但是適用於窗口
概括:
Style Sheets是文字性的設定,對於整個應用程序可以使用QApplication::setStyleSheet() 或者對應一個窗口可以使用QWidget::setStyleSheet(),如果好幾個樣式表在不同的層次上設定,
QT將會集合所有的樣式表來設定外觀,這稱作級串聯
例如:下面的樣式表指定所有的QLineEdit應該用黃色作爲他們的背景顏色,所有的核對框應該用紅色作爲他們的文本顏色
- QLineEdit { background: yellow }
- QCheckBox { color: red }
對於單獨使用QPalette很難完成的定製,樣式表可以指定樣式表作用於當前窗口樣式頂部,這意味這應用程序講看起來儘可能的自然,但是任何樣式表系統 參數應該考慮,不像QPalette那樣,樣式表提供檢查,如果你設定了一個按鈕的背景顏色爲紅色,你應該確定在所有的平臺按鈕將會有一個紅色的背景,除 此,Qt Designer提供樣式表集成環境,使得在不同的窗口樣式中更容易看到樣式表的效果
此外,樣式表可以用來爲你的應用程序提供一個出衆的外觀,不需要使用子類QStyle,例如,可以指定任意的圖片爲單選按鈕和核對按鈕,來使它們出衆,使 用這個技術,也可以獲得輔助的定製,這將使用幾個子類,例如指定style hint(樣式暗示),可以參看例子 Style Sheet。
當樣式表有效時候,使用QWidget::style()可以返回QStyle,
樣式表語法:
樣式表語法基本和HTML CSS語法一致。
樣式表包含了樣式規則序列,樣式規則有一個<selector>和<declaration>組成,<selector>指定哪些窗口將會被這些規則影響,<declaration>指定哪些屬性將會被設定在窗口上,例如
QPushButton{color:red}
在上面的,規則中,QPushButton是<selector>,{color:red}是<declaration>,這個規則指定QPushButton和他的子類將使用紅色作爲前景顏色,就是字體顏色,並且對大小寫沒有分別,對於
color,ColoR,COLOR是一樣的。
幾個<selector>可以同時被列出,使用逗號","來分開各個<selector>,例如:
QPushButton, QLineEdit, QComboBox { color: red }
<declaration>部分是一對 屬性:值 對,用{}來括起來,使用分號來分開各個屬性,例如
QPushButton { color: red; background-color: white }
可以參看Qt Style Sheets Reference來查看部件以及樣式表的屬性列表
關於樣式表的級聯屬性
看下面代碼的不同
- btn1->setStyleSheet("QPushButton{color:red}"); //設定前景顏色,就是字體顏色
- btn1->setStyleSheet("QPushButton{background:yellow}"); //設定背景顏色爲紅色
- btn1->setStyleSheet("QPushButton{color:red;background:yellow}");
所以當設定一個部件時候,要在同一個>setStyleSheet()中完全寫出來。
源代碼示例:
- Dialog::Dialog(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::Dialog)
- {
- ui->setupUi(this);
- this->setWindowFlags(this->windowFlags()&Qt::WindowMaximizeButtonHint&Qt::WindowMinimizeButtonHint); //爲對話框添加上最大化和最小化按鈕
- // layout=new QBoxLayout(this);
- layout1=new QGridLayout(this);
- btn1=new QPushButton(this);
- btn1->setStyleSheet("QPushButton{color:red;background:yellow}"); //設定前景顏色,就是字體顏色
- // btn1->setStyleSheet("QPushButton{background:yellow}");
- btn1->setText("Button1");
- btn2=new QPushButton(this);
- btn2->setStyleSheet("QPushButton{color:red;background-color:rgb(200,155,100)}"); //使用rgb來設定背景顏色
- btn2->setText("Button2");
- btn3=new QPushButton(this);
- btn3->setStyleSheet("QPushButton{background-image:url(image/1.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-attachment: fixed;background-attachment: fixed;;background-clip: padding}");
- //設定按鈕的背景圖片,background-repeat可以設定背景圖片的重複規則,這裏設定僅在xy方向都重複,所以圖片會被重複一次
- //background-position用來設定圖片的位置,是左(left)還是右(right),還是在中間(center),是上(top)還是底部(bottom)
- //background-attachment用來這定背景圖片是否捲動或者和窗口大小相匹配,默認是捲動的
- btn3->setText("Button3");
- btn4=new QPushButton(this);
- btn4->setStyleSheet("QPushButton{border: 3px solid red;border-radius:8px}"); //設定邊框寬度以及顏色
- //可以使用border-top,border-right,border-bottom,border-left分別設定按鈕的上下左右邊框,
- //同樣有border-left-color, border-left-style, border-left-width.等分別來設定他們的顏色,樣式和寬度
- //border-image用來設定邊框的背景圖片。
- //border-radius用來設定邊框的弧度。可以設定圓角的按鈕
- btn4->setText("Button4");
- //字體設定
- //font-family來設定字體所屬家族,
- //font-size來設定字體大小
- //font-style來設定字體樣式
- //font-weight來設定字體深淺
- //height用來設定其高低
- //selection-color用來設定選中時候的顏色
- edit1=new QLineEdit(this);
- edit1->setStyleSheet("QLineEdit{font: bold italic large /"Times New Roman/";font-size:25px;color:rgb(55,100,255);height:50px;border:4px solid rgb(155,200,33);border-radius:15px;selection-color:pink}");
- //父窗口的設定
- //icon-size來設定圖片大小
- this->setWindowIcon(QIcon("image/1.png"));
- this->setStyleSheet("QWidget{background:write url(image/2.png);icon-size:20px 5px}"); //設定整個對話框的背景顏色
- // this->setStyleSheet("QWidget{icon-size:20px 5px}");
- layout1->addWidget(btn1,0,0);
- layout1->addWidget(btn2,0,1);
- layout1->addWidget(btn3,1,0);
- layout1->addWidget(btn4,1,1);
- layout1->addWidget(edit1,2,0);
- }