您的位置:首页 > 编程语言 > Qt开发

QT StyleSheet(3)--QSS示例

2011-05-26 18:23 791 查看
QT StyleShee
from http://lhzryy163.blog.163.com/blog/static/112672575201102535954575/
by lhzryy1 2011-01-25 15:59:54

使用setStyleSheet来设置图形界面的外观:
QT Style Sheets是一个很有利的工具,允许定制窗口的外观,
此外还可以用子类QStyle来完成,他的语法很大比重来源于html的CSS,但是适用于窗口
概括:
Style Sheets是文字性的设定,对于整个应用程序可以使用QApplication::setStyleSheet() 或者对应一个窗口可以使用QWidget::setStyleSheet(),如果好几个样式表在不同的层次上设定,
QT将会集合所有的样式表来设定外观,这称作级串联
例如:下面的样式表指定所有的QLineEdit应该用***作为他们的背景颜色,所有的核对框应该用红色作为他们的文本颜色

QLineEdit { background: yellow }

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来查看部件以及样式表的属性列表
关于样式表的级联属性
看下面代码的不同

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: