使用setStyleSheet來設置圖形界面的外觀: QT Style Sheets是一個很有利的工具,允許定制窗口的外觀, 此外還可以用子類QStyle來完成,他的語法很大比重來源于html的CSS,但是適用于窗口 概括: Style Sheets是文字性的設定,對于整個應用程序可以使用
QApplication::setStyleSheet() 或者對應一個窗口可以使用QWidget::setStyleSheet
(),如果好幾個樣式表在不同的層次上設定, QT將會集合所有的樣式表來設定外觀,這稱作級串聯(lián) 例如:下面的樣式表指定所有的QLineEdit應該用黃色作為他們的背景顏色,所有的核對
框應該用紅色作為他們的文本顏色 QLineEdit { background: yellow } QCheckBox { color: red } 對于這種定制,樣式表比palette調色板更強大,例如使用QPalette::Button role來設
定一個按鈕為紅色可能引起危險 對于單獨使用QPalette很難完成的定制,樣式表可以指定 樣式表作用于當前窗口樣式頂部,這意味這應用程序講看起來盡可能的自然,但是任何
樣式表系統(tǒng)參數(shù)應該考慮,不像QPalette那樣,樣式表提供檢查,如果你設定了一個按
鈕的背景顏色為紅 色,你應該確定在所有的平臺按鈕將會有一個紅色的背景,除此,Qt Designer提供樣式
表集成環(huán)境,使得在不同的窗口樣式中更容易看到樣式表的效果 此外,樣式表可以用來為你的應用程序提供一個出眾的外觀,不需要使用子類QStyle,例
如,可以指定任意的圖片為單選按鈕和核對按鈕,來使它們出眾,使用這個技術,也可
以獲得輔助的定 制,這將使用幾個子類,例如指定style hint(樣式暗示),可以參看例子 Style Sheet。 當樣式表有效時候,使用QWidget::style()可以返回QStyle, 樣式表語法: 樣式表語法基本和HTML CSS語法一致。 樣式表包含了樣式規(guī)則序列,樣式規(guī)則有一個<selector>和<declaration>組成,
<selector>指定哪些窗口將會被這些規(guī)則影響,<declaration>指定哪些屬性將會被設定
在窗口上,例如 QPushButton{color:red} 在上面的,規(guī)則中,QPushButton是<selector>,{color:red}是<declaration>,這個規(guī)
則指定QPushButton和他的子類將使用紅色作為前景顏色,就是字體顏色,并且對大小寫
沒有分別,對于 color,ColoR,COLOR是一樣的。 幾個<selector>可以同時被列出,使用逗號","來分開各個<selector>,例如: QPushButton, QLineEdit, QComboBox { color: red } <declaration>部分是一對 屬性:值 對,用{}來括起來,使用分號來分開各個屬性,
例如 QPushButton { color: red; background-color: white } 可以參看Qt Style Sheets Reference來查看部件以及樣式表的屬性列表 關于樣式表的級聯(lián)屬性 看下面代碼的不同 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可以設定背景圖片的重復規(guī)則,這里設
定僅在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用來設定邊框的弧度??梢栽O定圓角的按鈕 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); } 這里只給出來widget主窗口的cpp文件,運行得到的結果如下圖
 我們看到連粘貼 復制板都變成了使用樣式表來設定的樣式  關于使用樣式表 setStyleSheet()) 來設定窗口樣式的時候因該注意的事項,可以參看 http://blog.csdn.net/xie376450483/archive/2010/08/17/5818759.aspx
|