Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

c++ - How to send artificial QKeyEvent to QWebEngineView?

Context: I'm creating a small web browser with a custom on-screen keyboard.

It was working almost fine with Qt WebKit (QWeb* classes) but there were crashes attributed to bugs in WebKit... which won't be fixed after Qt 5.4.0 since they're moving to Qt WebEngine.

So I decided to move the stuff to Qt WebEngine (QWebEngine* classes), following the short webkit->webengine transition guide. Following the caveat section on QWebElement, I have worked my way around showing/hiding the on-screen keyboard (which now requires running async. JS code). But I'm scratching my head on how to send artificial key events to the web page.

I have tried the some stuff:

  • QCoreApplication::postEvent(m_webview, event) doesn't do anything, when it was working with the old QWeb stuff;
  • It's possible to send keys by running JavaScript but I find this too dirty

Thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...