• 6802阅读
  • 6回复

[提问]重写QPushButton后,setFlat失效是怎么回事 [复制链接]

上一主题 下一主题
离线guantou319
 
只看楼主 倒序阅读 楼主  发表于: 2012-02-02
我自己写了一个Button类继承QPushButton,重写了button的外观和鼠标按钮离开等事件,但是我这个类的对象在进行其他设置的时候就失效了,是怎么回事啊?
Button button1;
button1->setFlat(true);
button1->setDown(true);
上面两个方法都不起作用了。
离线wxj120bw

只看该作者 1楼 发表于: 2012-02-02
回 楼主(guantou319) 的帖子
flat : bool
This property holds whether the button border is raised.

This property's default is false. If this property is set, most styles will not paint the button background unless the button is being pressed. setAutoFillBackground() can be used to ensure that the background is filled using the QPalette::Button brush.

down : bool
This property holds whether the button is pressed down.

If this property is true, the button is pressed down. The signals pressed() and clicked() are not emitted if you set this property to true. The default is false.
上面摘抄qt帮助 按钮离开事件失效应该跟Flat有关 重写了button的外观应该不会失效吧
离线淳于半邪

只看该作者 2楼 发表于: 2012-02-02
new一个对象的时候需要与你构造保持一致,
Button button1;

估计是new出错了
离线jdwx

只看该作者 3楼 发表于: 2012-02-02
回 楼主(guantou319) 的帖子
如果,重写了button的外观 = 重写了paintEvent,那么原来控制外观的属性都不好用了。
全部要自己重写。
QAbstractButton 和 QPushButton 的区别就是 paintEvent。
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
离线guantou319
只看该作者 4楼 发表于: 2012-02-02
回 3楼(jdwx) 的帖子
我基本上是照着这个重写的http://www.qtcentre.org/wiki/index.php?title=AeroButton
setFlat()要重写感觉好复杂啊,还涉及到鼠标悬浮和离开的事件。能给个思路吗?
另外QAbstractButton没用过,不熟啊
离线jdwx

只看该作者 5楼 发表于: 2012-02-02
回 4楼(guantou319) 的帖子
QAbstractButton 是 QPushButton 的基类,   QPushButton只是写了paintEvent。
QAbstractButton 的paintEvent是纯虚函数。

setFlat();一定要重写。
方法:增加一个bool变量
剩下的就是paintEvent里的代码了。


void enterEvent(QEvent * e);鼠标进入按钮
void leaveEvent(QEvent * e);鼠标移出按钮
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
离线guantou319
只看该作者 6楼 发表于: 2012-02-02
回 5楼(jdwx) 的帖子
太感谢啦  问题解决了
快速回复
限100 字节
 
上一个 下一个