- 先安装事件过滤器
MyLable::MyLable(QWidget *parent) : QLabel(parent)
{
//设置追踪模式
//this->setMouseTracking(true);
this->installEventFilter(this);
} - 重写过滤器
virtual bool eventFilter(QObject* watched,QEvent* event);
bool MyLable::eventFilter(QObject *watched, QEvent *event)
{
//判断是当前控件触发了
if(watched == this)
{
//判断触发了哪个事件
if(event->type() == QEvent::MouseButtonPress)
{
qDebug()<<“过滤器”<<endl;
return true;
}
}
else
{
return QLabel::eventFilter(watched,event);
}
}
04-18
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-17
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-15
1730
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-22
1151
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-02
1221
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)