最近有个需求,需要在QWebEngineView加载网页后模拟一个点击,直接创建一个QMouseEvent同过QApplication::sendEvent或者QApplication::postEvent发送给QWebEngineView,发现QWebEngineView中没有执行QWebEngineView::mousePressEvent和QWebEngineView::mouseReleaseEvent,通过查找资料,发现QWebEngineView在加载界面后,被本身的child接收了改事件
解决办法:同过重载event,截取QEvent::ChildAdded事件,通过智能指针QPointer<QOpenGLWidget>保存这个child指针(类型QOpenGLWidget),我们可以通过send鼠标信号到这个child即可模拟正常的鼠标点击事件
bool CWebEngineView::event(QEvent *event)
{
if(event->type() == QEvent::ChildAdded)
{
QChildEvent* childEvent = (QChildEvent*)event;
if(childEvent)
{
QObject *child = childEvent->child();
qDebug()<<"className"<<child->metaObject()->className();
QOpenGLWidget *w =