Qt setStyleSheet的神奇

来源:互联网 发布:短信任意显示软件 编辑:程序博客网 时间:2024/05/20 12:23
Qt中设置按钮或QWidget的外观是,可以使用QT Style Sheets来进行设置,非常方便。
可以用setStyleSheet("font: bold; font-size:20px; color: rgb(241, 70, 62); background-color: green");来进行设置,其他的样式介绍如下:
font: bold;是否粗体显示
border-image:"";用来设定边框的背景图片。
border-radius:5px;用来设定边框的弧度。可以设定圆角的按钮
border-width: 1px;边框大小


font-family:"";来设定字体所属家族,
font-size:20px;来设定字体大小
font-style:"";来设定字体样式
font-weight:20px;来设定字体深浅
 background-color: green;设置背景颜色
background:transparent;设置背景为透明
color:rgb(241, 70, 62);设置前景颜色
selection-color:rgb(241, 70, 62);用来设定选中时候的颜色


可以使用border-top,border-right,border-bottom,border-left分别设定按钮的上下左右边框,

同样有border-left-color, border-left-style, border-left-width.等分别来设定他们的颜色,样式和宽度


用setStyleSheet来设置图形界面的外观:
  QT Style Sheets是一个很有利的工具,允许定制窗口的外观,此外还可以用子类QStyle来完成,他的语法很大比重来源于html的CSS,但是适用于窗口。

概括:

  Style Sheets是文字性的设定,对于整个应用程序可以使用QApplication::setStyleSheet() 或者对应一个窗口可以使用QWidget::setStyleSheet(),如果好几个样式表在不同的层次上设定,QT将会集合所有的样式表来设定外观,这称作级串联

1 //例如:下面的样式表指定所有的QLineEdit应该用黄色作为他们的背景颜色,所有的核对框应该用红色作为他们的文本颜色2 QLineEdit { background: yellow }3 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; font-family: Arial; line-height: 26px;">可以参看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(...)中完全写出来,否则对于该部件来讲,只有最后一个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 14     btn2=new QPushButton(this);15     btn2->setStyleSheet("QPushButton{color:red; //使用rgb来设定背景颜色16     btn2->setText("Button2");17 18      btn3=new QPushButton(this);19      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}");20      //设定按钮的背景图片,background-repeat可以设定背景图片的重复规则,这里设定仅在xy方向都重复,所以图片会被重复一次21      //background-position用来设定图片的位置,是左(left)还是右(right),还是在中间(center),是上(top)还是底部(bottom)22      //background-attachment用来这定背景图片是否卷动或者和窗口大小相匹配,默认是卷动的23      btn3->setText("Button3");24 25      btn4=new QPushButton(this);26      btn4->setStyleSheet("QPushButton{border: 3px solid red;border-radius:8px}"); //设定边框宽度以及颜色27      //可以使用border-top,border-right,border-bottom,border-left分别设定按钮的上下左右边框,28      //同样有border-left-color, border-left-style, border-left-width.等分别来设定他们的颜色,样式和宽度29      //border-image用来设定边框的背景图片。30      //border-radius用来设定边框的弧度。可以设定圆角的按钮31      btn4->setText("Button4");32 33      //字体设定34      //font-family来设定字体所属家族,35      //font-size来设定字体大小36      //font-style来设定字体样式37      //font-weight来设定字体深浅38      //height用来设定其高低39      //selection-color用来设定选中时候的颜色40      edit1=new QLineEdit(this);41      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}");42 43      //父窗口的设定44      //icon-size来设定图片大小45      this->setWindowIcon(QIcon("image/1.png"));46       this->setStyleSheet("QWidget{background:write url(image/2.png);icon-size:20px 5px}");  //设定整个对话框的背景颜色47 //      this->setStyleSheet("QWidget{icon-size:20px 5px}");48     layout1->addWidget(btn1,0,0);49     layout1->addWidget(btn2,0,1);50     layout1->addWidget(btn3,1,0);51     layout1->addWidget(btn4,1,1);52      layout1->addWidget(edit1,2,0);53 }54 55  

原创粉丝点击