How to send artificial QKeyEvent to QWebEngineView?
I guess the only possibility to achieve this right now would be to
make use of QAction
to send an event to the WebView by using for example something like that:
connect( this , SIGNAL( keyPressed( int ) ) , &m_webview , SLOT( handleKey( int ) ) );
I suppose the functionality will be added in Qt 5.5.1 as you can see below:
https://codereview.qt-project.org/#/c/104901/
This code work fine
for(auto* child : ui->webEngineView->children() ) {
int key = Qt::Key_V; //or some other
QKeyEvent pressEvent = QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, QKeySequence(key).toString());
QKeyEvent releaseEvent = QKeyEvent(QEvent::KeyRelease, key, Qt::NoModifier);
qApp->sendEvent(child, &pressEvent);
qApp->sendEvent(child, &releaseEvent);
}
Despite original question is one year old, it is still actual for those who like me decided to move (at last!) from QWebKit to QWebEngine (Qt 5.5 - 5.6b). Here is a dirty solution that requires existing webenginepage->view(). This is for mouse events, and it would not be a big surprise if it's not situated for keyboard events:
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 );
}
Inspired by Using QWebEngine to render an image and How can I get paint events with QtWebEngine? and googling.