• 20669阅读
  • 15回复

请样改变QLabel的背影色? [复制链接]

上一主题 下一主题
离线xcgwy
 
只看楼主 倒序阅读 楼主  发表于: 2006-07-22
QLabel *label=new QLabel(w);
QPalette pal = label->palette();
pal.setBrush(QPalette::Background, QBrush(Qt::red));
pal.setColor(QPalette::Background, Qt::red);
label->setPalette( pal );
这样不好使。请问如何才能设置label的背景色?
离线shiroki

只看该作者 1楼 发表于: 2006-07-24
setBackgroundColor
--
shiro is White
ki is tree
http://www.cuteqt.com
论坛 http://www.cuteqt.com/bbs
博客 http://www.cuteqt.com/blog
博客镜像: http://sites.cuteqt.com/cuteqt
Linux/Qt/嵌入式讨论群 http://qun.qq.com/air/5699823
离线xcgwy
只看该作者 2楼 发表于: 2006-07-25
QT4.1.4中没有这个函数
离线liyao32
只看该作者 3楼 发表于: 2006-07-26
QPalette palette;
palette.setColor(QPalette::Active, static_cast<QPalette::ColorRole>(10), QColor(255,0,0));
label->setPalette(palette);

这样就可以了啊。
离线liyao32
只看该作者 4楼 发表于: 2006-07-26
static_cast<QPalette::ColorRole>(10),可以写成QPalette::Background
离线xcgwy
只看该作者 5楼 发表于: 2006-07-27
不行的,我上面的代码不就是这样吗?
离线liyao32
只看该作者 6楼 发表于: 2006-07-27
我的就是这么做的啊?可以改变颜色。但是不知道为什么我得按钮改变不了背景色
离线matrix_k7

只看该作者 7楼 发表于: 2006-07-28
QLabel的背影色是用QPalette::Base来设置,QPushButton在Assistant中说是用QPalette::Button来设置,但我试了一下好像不行,底下链接也是讨论这个问题的
http://www.qtcentre.org/forum/f-qt-programming-2/t-change-background-color-of-qpushbutton-2165.html
离线liyao32
只看该作者 8楼 发表于: 2006-07-28
谢谢楼上的,我得问题已经解决,就是在这个帖子里提到的Style问题,只需要在main函数的开始加上QApplication::setStyle(new QWindowsXPStyle);这句就可以了。
离线yangjr053858
只看该作者 9楼 发表于: 2009-05-06
可以实现的,[就像一楼说的,只不过要在之前加一句label->setAutoFillBackground (true);
离线yangjr053858
只看该作者 10楼 发表于: 2009-05-06
补充一下:
Qt assistant :
        The palette's background color will only have an effect on the appearance of the widget if the autoFillBackground property is set true;

完整实现:
   label->setAutoFillBackground (true);
      QPalette palette;
     palette.setColor(QPalette::Active, static_cast<QPalette::ColorRole>(10), QColor(255,0,0));
     label->setPalette(palette);

楼上说的在main函数的开始加上QApplication::setStyle(new QWindowsXPStyle);完全没必要。牛头不对马嘴!
离线robertkun

只看该作者 11楼 发表于: 2011-06-13
                                              简单的生活使人快乐!
离线msccreater

只看该作者 12楼 发表于: 2012-11-29
引用第10楼yangjr053858于2009-05-06 10:11发表的  :
补充一下:
Qt assistant :
        The palette's background color will only have an effect on the appearance of the widget if the autoFillBackground property is set true;
完整实现:
.......

问一下参数里面的QPalette:Active, static_cast<QPalette::ColorRole>(10) 是什么意思呢?
离线jdwx

只看该作者 13楼 发表于: 2012-11-29
回 12楼(msccreater) 的帖子
static_cast<QPalette::ColorRole>(10) = QPalette::Window
就是:QPalette::Window这个常量,不知道为什么写成那样?

发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
离线msccreater

只看该作者 14楼 发表于: 2012-11-29
回 10楼(yangjr053858) 的帖子
我使用楼主的代码没有实现呢。。。求解释啊,我是把label放在QTableWidget里面了,而且对QTableWidget的横向表头设置过一个stylesheet,不知道有关系没
离线kimtaikee

只看该作者 15楼 发表于: 2012-12-13
  1. QLabel {
  2.    background-color:rgb(43,55,123);
  3. }


快速回复
限100 字节
 
上一个 下一个