其他分享
首页 > 其他分享> > QTextEdit和QTextBrowser点击事件响应

QTextEdit和QTextBrowser点击事件响应

作者:互联网

头文件重载过滤事件函数

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 true;
            }
        }
    }
    else if (obj == ui->textEdit_3->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 true;
            }
        }
    }
    else
    {
        // pass the event on to the parent class
        return Widget2::eventFilter(obj, event);
    }
    return false;
}

 

标签:obj,QTextBrowser,QTextEdit,QEvent,点击,mouseEvent,return,type,event
来源: https://blog.csdn.net/u011625775/article/details/100738319