【问题标题】:How to send artificial QKeyEvent to QWebEngineView?如何将人工 QKeyEvent 发送到 QWebEngineView?
【发布时间】:2015-03-09 19:02:10
【问题描述】:

上下文:我正在创建一个带有自定义屏幕键盘的小型网络浏览器。

它在 Qt WebKit (QWeb* classes) 上几乎可以正常工作,但是由于 WebKit 中的错误而导致崩溃...由于它们正在迁移到 Qt WebEngine,因此在 Qt 5.4.0 之后将无法修复。

所以我决定按照简短的 webkit->webengine 转换指南将这些东西移到 Qt WebEngine(QWebEngine* 类)。 在QWebElement 上的警告部分之后,我已经解决了显示/隐藏屏幕键盘(现在需要运行 async.JS 代码)的方法。 但我对如何将人工键事件发送到网页感到头疼。

我已经尝试了一些东西:

  • QCoreApplication::postEvent(m_webview, event) 在处理旧的 QWeb 东西时什么也不做;
  • 可以通过运行 JavaScript 来发送密钥,但我觉得这太脏了

谢谢,

【问题讨论】:

  • 你用这个有没有成功?

标签: c++ qt qwebview qtwebengine


【解决方案1】:

我想现在实现这一目标的唯一可能性是 使用QAction 向 WebView 发送事件,例如:

connect( this , SIGNAL( keyPressed( int ) ) , &m_webview , SLOT( handleKey( int ) ) );

我想该功能将在 Qt 5.5.1 中添加,如下所示:

https://codereview.qt-project.org/#/c/104901/

【讨论】:

    【解决方案2】:

    尽管最初的问题已经存在一年了,但对于像我这样决定(终于!)从 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 );
    }
    

    灵感来自 Using QWebEngine to render an imageHow can I get paint events with QtWebEngine? 和谷歌搜索。

    【讨论】:

      【解决方案3】:

      这段代码运行良好

       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);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多