赞
踩
通过样式表来设置按钮的样子,包含正常显示状态,按下状态,悬停状态,这是设置当前窗口下全局的按钮,可以根据自己的需要进行修改。
- setStyleSheet(
- //正常状态样式
- "QPushButton{"
- "background-color:rgba(100,225,100,30);"//背景色(也可以设置图片)
- "border-style:outset;" //边框样式(inset/outset)
- "border-width:4px;" //边框宽度像素
- "border-radius:10px;" //边框圆角半径像素
- "border-color:rgba(255,255,255,30);" //边框颜色
- "font:bold 10px;" //字体,字体大小
- "color:rgba(0,0,0,100);" //字体颜色
- "padding:6px;" //填衬
- "}"
- //鼠标按下样式
- "QPushButton:pressed{"
- "background-color:rgba(100,255,100,200);"
- "border-color:rgba(255,255,255,30);"
- "border-style:inset;"
- "color:rgba(0,0,0,100);"
- "}"
- //鼠标悬停样式
- "QPushButton:hover{"
- "background-color:rgba(100,255,100,100);"
- "border-color:rgba(255,255,255,200);"
- "color:rgba(0,0,0,200);"
- "}");

效果:
如果想设置单个的按钮样式,则把开始的一行修改成:
- ui->pushButton_2->setStyleSheet(
- //正常状态样式
通过UI指向按钮进行修改样式表就可以了。
在Qt的中也可以通过把样式表的内存存储到文件中,再进行读取设置,就可以实现进行换肤的功能:
- //这是在Qt的资源下的文件,可以不用在资源下
- QFile file(":/my.qss");
- //只读方式打开文件
- file.open(QFile::ReadOnly);
- //读取文件的所有内容,并转换成QString类型
- QString styleSheet = tr(file.readAll());
- //设置样式表
- setStyleSheet(styleSheet);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。