.h
1 //给label添加事件
2 bool eventFilter(QObject *obj, QEvent *event);
.cpp
1 bool MainWindow::eventFilter(QObject *obj, QEvent *event){
2 //mouse button pressed
3 if (event->type() == QEvent::MouseButtonPress){
4 QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
5 if(mouseEvent->button() == Qt::LeftButton){
6 if(obj == ui->label_avator){
7 qDebug()<<"在这里写点击事件";
8 return true;
9 }else{
10 return false;
11 }
12 }else{
13 return false;
14 }
15 }else{
16 return QObject::eventFilter(obj, event);
17 }
18 }
最后给QLabel添加事件过滤
ui->label_avator->installEventFilter(this);