我们知道,要对Qt窗口组件进行事件过滤可以重写eventFilter函数,并且需要将其InstallEventFilter例如:
MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
setWindowTitle(u8"example");
view.setContextMenuPolicy(Qt::NoContextMenu);//禁用右键
view.installEventFilter(this); //view 是一个组件窗口
layout.addWidget(&view);
setLayout(&layout);
show();
}
但是QWebEngineView在加载界面后,被本身的child接收了鼠标键盘事件信息,与其他组件不同,需要对其child进行installEventFilter。
解决方法可参考以下链接:
class WebEngineView : public QWebEngineView
{
Q_OBJECT
pubilc:
WebEngineView(){}
bool event(QEvent * ev){
if(evt->type()== QEvent::ChildPolished ){
QChildEvent* ce=static_cast<QChildEvent*>(evt);
child = ce->child();
if(child){
child->installEventFilter(this);
}
}
return QWebEngineView::event(evt);
}
bool eventFilter(QObject *obj, QEvent *ev)
{
if (ev->type() == QEvent::Wheel ){
qDebug()<<"wheel---";
if (QApplication::keyboardModifiers () == Qt::ControlModifier){
qDebug()<<"wheel+ctrl---";
return true;
}
}
return QWebEngineView::eventFilter(obj,ev);
}
private:
QObject* child;
};