尽管最初的问题已经存在一年了,但对于像我一样决定(终于!)从 QWebKit 迁移到 QWebEngine(Qt 5.5 - 5.6b)的人来说,它仍然是实际的。这是一个肮脏的解决方案,需要现有的 webenginepage->view()。这是针对鼠标事件的,如果它不用于键盘事件,那就不足为奇了:
void Whatever::sendMouseEvent( QObject* targetObj, QMouseEvent::Type type, const QPoint& pnt ) const
{
QMouseEvent event( type, pnt, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
QApplication::sendEvent( targetObj, &event );
}
void Whatever::sendMouseClick( QObject* targetObj, const QPoint& pnt ) const
{
sendMouseEvent( targetObj, QMouseEvent::MouseMove, pnt );
sendMouseEvent( targetObj, QMouseEvent::MouseButtonPress, pnt );
sendMouseEvent( targetObj, QMouseEvent::MouseButtonRelease, pnt );
}
void Whatever::emulateMouseClick( const QPoint& pnt ) const
{
//-- right now (Qt 5.5 & 5.6) there is only one child -
//-- QtWebEngineCore::RenderWidgetHostViewQtDelegateWidget
//-- but it could change in future
Q_FOREACH( QObject* obj, mWebEnPage->view()->children() ) //-- ACHTUNG! Check mWebEnPage->view() in real code!
if( qobject_cast<QWidget*>( obj ) )
sendMouseClick( obj, pnt );
}
灵感来自
使用 QWebEngine 渲染图像
以及
如何使用 QtWebEngine 获取绘制事件?
和谷歌搜索。