QT StyleShee


QT StyleShee
by lhzryy1   2011-01-25 15:59:54 
使用setStyleSheet來設置圖形界面的外觀:
QT Style Sheets是一個很有利的工具,允許定製窗口的外觀,
此外還可以用子類QStyle來完成,他的語法很大比重來源於html的CSS,但是適用於窗口
概括:
Style Sheets是文字性的設定,對於整個應用程序可以使用QApplication::setStyleSheet() 或者對應一個窗口可以使用QWidget::setStyleSheet(),如果好幾個樣式表在不同的層次上設定,
QT將會集合所有的樣式表來設定外觀,這稱作級串聯
例如:下面的樣式表指定所有的QLineEdit應該用黃色作爲他們的背景顏色,所有的核對框應該用紅色作爲他們的文本顏色
  1. QLineEdit { background: yellow }
  2. QCheckBox { color: red }
複製代碼
對於這種定製,樣式表比palette調色板更強大,例如使用QPalette::Button role來設定一個按鈕爲紅色可能引起危險
對於單獨使用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來查看部件以及樣式表的屬性列表
關於樣式表的級聯屬性
看下面代碼的不同
  1. btn1->setStyleSheet("QPushButton{color:red}"); //設定前景顏色,就是字體顏色
  2. btn1->setStyleSheet("QPushButton{background:yellow}"); //設定背景顏色爲紅色

  1. btn1->setStyleSheet("QPushButton{color:red;background:yellow}");
第一個代碼只能顯示黃色背景,第二個確實紅色字體,黃色背景,
所以當設定一個部件時候,要在同一個>setStyleSheet()中完全寫出來。
源代碼示例:
  1. Dialog::Dialog(QWidget *parent) :
  2.     QDialog(parent),
  3.     ui(new Ui::Dialog)
  4. {
  5.     ui->setupUi(this);
  6.     this->setWindowFlags(this->windowFlags()&Qt::WindowMaximizeButtonHint&Qt::WindowMinimizeButtonHint); //爲對話框添加上最大化和最小化按鈕
  7. //    layout=new QBoxLayout(this);
  8.     layout1=new QGridLayout(this);
  9.     btn1=new QPushButton(this);
  10.     btn1->setStyleSheet("QPushButton{color:red;background:yellow}"); //設定前景顏色,就是字體顏色
  11. //    btn1->setStyleSheet("QPushButton{background:yellow}");
  12.     btn1->setText("Button1");
  13.     btn2=new QPushButton(this);
  14.     btn2->setStyleSheet("QPushButton{color:red;background-color:rgb(200,155,100)}"); //使用rgb來設定背景顏色
  15.     btn2->setText("Button2");
  16.      btn3=new QPushButton(this);
  17.      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}");
  18.      //設定按鈕的背景圖片,background-repeat可以設定背景圖片的重複規則,這裏設定僅在xy方向都重複,所以圖片會被重複一次
  19.      //background-position用來設定圖片的位置,是左(left)還是右(right),還是在中間(center),是上(top)還是底部(bottom)
  20.      //background-attachment用來這定背景圖片是否捲動或者和窗口大小相匹配,默認是捲動的
  21.      btn3->setText("Button3");
  22.      btn4=new QPushButton(this);
  23.      btn4->setStyleSheet("QPushButton{border: 3px solid red;border-radius:8px}"); //設定邊框寬度以及顏色
  24.      //可以使用border-top,border-right,border-bottom,border-left分別設定按鈕的上下左右邊框,
  25.      //同樣有border-left-color, border-left-style, border-left-width.等分別來設定他們的顏色,樣式和寬度
  26.      //border-image用來設定邊框的背景圖片。
  27.      //border-radius用來設定邊框的弧度。可以設定圓角的按鈕
  28.      btn4->setText("Button4");
  29.      //字體設定
  30.      //font-family來設定字體所屬家族,
  31.      //font-size來設定字體大小
  32.      //font-style來設定字體樣式
  33.      //font-weight來設定字體深淺
  34.      //height用來設定其高低
  35.      //selection-color用來設定選中時候的顏色
  36.      edit1=new QLineEdit(this);
  37.      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}");
  38.      //父窗口的設定
  39.      //icon-size來設定圖片大小
  40.      this->setWindowIcon(QIcon("image/1.png"));
  41.       this->setStyleSheet("QWidget{background:write url(image/2.png);icon-size:20px 5px}");  //設定整個對話框的背景顏色
  42. //      this->setStyleSheet("QWidget{icon-size:20px 5px}");
  43.     layout1->addWidget(btn1,0,0);
  44.     layout1->addWidget(btn2,0,1);
  45.     layout1->addWidget(btn3,1,0);
  46.     layout1->addWidget(btn4,1,1);
  47.      layout1->addWidget(edit1,2,0);
  48. }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章