1,QLineEdit 添加事件触发内容:(放在构造函数上即可)
ui->qlineedit_1->installEventFilter(this);
ui->qlineedit_1->setMouseTracking(true);
ui->qlineedit_2->installEventFilter(this);
ui->qlineedit_2->setMouseTracking(true);
2,重写eventFilter函数,声明eventFilter 为protected函数
bool Widget::eventFilter(QObject *watched, QEvent *event)
{
if( watched == ui->qlineedit_1 || watched == ui->qlineedit_1 ) {
if( event->type() == QEvent::MouseButtonPress ) {
//由于我需要传递参数,但是监控的便签有2个,所以要找到指定的qlineedit
QLineEdit *point = this->findChild<QLineEdit *>( watched->objectName() );
//处理鼠标点击事件
DealeMousePress(point);
return true;
} else {
return false;
}
}
return QWidget::eventFilter(watched,event);
}