qt中设置前景色和背景色

Qt Style Sheets Examples——定制前景色和背景色

例子取自:http://qt-project.org/doc/qt-4.8/stylesheet-examples.html

以lineEdit为例

(1)设置某个lineEdit的背景色为黄色

lineEdit->setStyleSheet (" font-size: 12px !important; line-height: 1.5 !important;">");

(2)设置一个应用项目中所有lineEdit的背景色均为黄色(line 4)

复制代码
1 int main(int argc, char *argv[])
2 {
3     QApplication a(argc, argv);
4     a.setStyleSheet ("QLineEdit { font-size: 12px !important; line-height: 1.5 !important;">");
5     Widget w;
6     w.show();
7     return a.exec();
8 }
复制代码

(3)设置某一个对话框中的所有lineEdit的背景色均为黄色

myDialog->setStyleSheet("QLineEdit {  font-size: 12px !important; line-height: 1.5 !important;">");

(4)设置lineEdit的文本颜色为红色

lineEdit->setStyleSheet ("color:red");

(5)综合实例

1 lineEdit->setStyleSheet (""
2                          "color:red;"
3                          "selection-color:blur;"
4                          "selection-backgroundcolor:green;");

注意!如果既要设置文本颜色为红色,又要设置背景色为黄色,不能这样写:

1 lineEdit->setStyleSheet (" font-size: 12px !important; line-height: 1.5 !important;">");
2 lineEdit->setStyleSheet ("color:red");

两个效果不是叠加的,后者会覆盖前者,就是说,文本颜色被设置为红色而背景色并不是黄色。要同时实现两种效果应该如上一个例子那样编写程序。

 

 2013-09-02 17:24:21

posted on 2015-05-29 14:11  areu-me  阅读(1649)  评论(0编辑  收藏  举报

导航