标题:我是新手,setStyleSheet的问题
作者:pangshengs
日期:2010-10-26 17:20
内容:
我用setStyleSheet的时候很疑惑,想做多个按钮,每个按钮点击的时候会切换同的png图,为什么用setStyleSheet的时候,只有一个是切换了,其它按钮没有切换图片呢?代码如下:
QPushButton* btn = new QPushButton(this);
btn->setGeometry(10, 10, 200, 200);
btn->setStyleSheet("QPushButton {border-image:url(/PocketMory1/png/off1.png); font-size:24px;} \
QPushButton:hover:pressed {border-image:url(/PocketMory1/png/on1.png);font-size:24px;} \
QPushButton:hover:!pressed {border-image:url(/PocketMory1/png/off1.png);font-size:24px;} ");
btn->setFlat(true);
btn->show();
QPushButton* btn2 = new QPushButton(this);
btn2->setGeometry(150, 150, 200, 200);
btn2->setStyleSheet("QPushButton {border-image:url(/PocketMory1/png/off2.png); font-size:24px;} \
QPushButton:hover:pressed {border-image:url(/PocketMory1/png/on2.png);font-size:24px;} \
QPushButton:hover:!pressed {border-image:url(/PocketMory1/png/off2.png);font-size:24px;} ");
btn2->setFlat(true);
btn2->show();
这样哪里不对呢?代码是在继承的对话框的初始化中。
#1 [dwhome 02-15 16:45]
问题解决了没?
告诉你一个方法:
在main.cpp中设置样表:
w.setStyleSheet(
"QToolButton:hover{ "
.............................................
这样所有QToolButton都变了,不用一个一个设置
#2 [dwhome 02-26 11:39]
看明白了,你是想让两个按钮显示不同的图标。
你那样做不行。
对不同的对象要先setObjectName,然后再setStyleSheet。
具体方法论坛里有,俺手懒,你自己找找吧。
#3 [dwhome 02-26 11:43]
好人做到底
http://www.qtcn.org/bbs/read.php?tid=32987
#4 回 3楼(dwhome) 的帖子 [haofan2009 08-18 13:22]
哥们人真好,偶喜欢