Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
static int num=0;
ui->setupUi(this);
tm = new QTimer(this);
tm->start(500);
connect(tm,&QTimer::timeout,[=](){
ui->label_2->setText(QString::number(num++));
});
// 给ui->label做事件过滤器拦截
// 给控件安装过滤器
ui->label->installEventFilter(this);
// 重写过滤器事件
}
bool Widget::eventFilter(QObject *watched, QEvent *event)
{
if(watched == ui->label){
if(event->type() == QEvent::MouseButtonPress){
QMouseEvent* ev = static_cast<QMouseEvent*>(event);
QString str = QString("鼠标摁下 x = %1 y = %2").arg(ev->x()).arg(ev->y());
qDebug()<<"鼠标摁下"<<str;
return true;
}
}
// 其他让父类处理
return QWidget::eventFilter(watched,event);
}