首页| 论坛| 消息

标题:qt的event事件
作者:夏惠霞
日期:2016-02-28 20:20
内容:

我自定义了了一个自己的pushbutton类,
class MyButton:public QPushButton
{
public:
explicit MyButton(QWidget *parent = 0);
bool event(QEvent *e);
};
MyButton::MyButton(QWidget *parent)
:QPushButton(parent){
}
bool MyButton::event(QEvent *e){
if(this->isEnabled() == true){
if(e->type() == QEvent::MouseButtonPress){
this->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1.000, stop:0 #07a8e1, stop:1 #036098);");
}
if(e->type() == QEvent::HoverEnter){
this->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1.000, stop:0 #07a8e1, stop:1 #05629a);");
}
if(e->type() == QEvent::HoverLeave){
this->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1.000, stop:0 rgba(6, 180, 242, 255), stop:1 rgba(2, 127, 202, 255));");
}
}
return true;
}
当我在程序中使用这个自定义的控件的时候,程序运行起来的时候空间居然看不到了。但是如果我把最后一行return true改为:return QPushButton::event(event),控件就可以看到并且正常运行,这是为什么?


#1 [yanwuyue 02-28 22:18]
除了你定义的动作,其它动作传回QPushButton
#2 [dosmlp 02-29 14:45]
因为这个事件处理函数里面还包含了界面重绘等其他事件,你要传回基类处理这些事件

回复 发表
主题 版块