头文件重载过滤事件函数
protected:
bool eventFilter(QObject *obj, QEvent *event);
//安装事件过滤器
ui->textBrowser_22->viewport()->installEventFilter(this);//安装事件过滤器
ui->textEdit_3->viewport()->installEventFilter(this);
//重载事件过滤器函数原型
bool Widget2::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->textBrowser_22->viewport())//指定某个QLabel
{
if (event->type() == QEvent::MouseButtonPress)//mouse button pressed
{
qDebug() << __FILE__<<__LINE__<< QString::number(event->type());
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if(mouseEvent->button() == Qt::LeftButton) //判断是否鼠标左键事件
{
return QWidget::eventFilter(obj, event);
}
}
}
//或者
// else if (( static_cast<QCustomPlot *>(obj))->objectName() == ui->customPlot->objectName())//指定某个QLabel
// {
// if (event->type() == QEvent::MouseButtonPress| QEvent::MouseMove | QEvent::MouseButtonRelease)
// {
// qDebug() << __FILE__<<__LINE__<< event->type();
// mouseMoveDirection(event->type());
return QWidget::eventFilter(obj, event);
// }
// }
return QWidget::eventFilter(obj, event);
}