【问题标题】:Qt WebEngine simulate Mouse EventQt WebEngine 模拟鼠标事件
【发布时间】:2017-03-28 10:54:34
【问题描述】:

我想在我的 Qt WebEngine 应用中模拟鼠标事件。

我使用 PyQt5.8 、QT5.8

这是我的代码:

    def mouse_click(self, x, y):
        point = QPoint(int(x),
                    int(y))
        eventp = QMouseEvent(QMouseEvent.MouseButtonPress,point,Qt.LeftButton,Qt.LeftButton,Qt.NoModifier)
        self.sendEvent(eventp)
        eventp = QMouseEvent(QMouseEvent.MouseButtonRelease,point,Qt.LeftButton,Qt.LeftButton,Qt.NoModifier)
        self.sendEvent(eventp)


     def sendEvent(self, event):
         recipient = self.webpage.view().focusProxy()
         recipient.grabKeyboard()
         self.application.sendEvent(recipient, event)
         recipient.releaseKeyboard()

我测试了它,但它不起作用。我可以确认元素上的鼠标光标,但没有发生鼠标单击事件。谁能帮帮我?

我使用的是 Mac OS 10.12.4,我用另一个演示测试它,我发现我无法捕捉鼠标事件,但我可以捕捉其他事件。有什么建议么?

【问题讨论】:

    标签: qt qtwebengine qmouseevent


    【解决方案1】:

    对于运行以下代码的 Qt 5.8:

    void LeftMouseClick(QWidget* eventsReciverWidget, QPoint clickPos)
    {
        QMouseEvent *press = new QMouseEvent(QEvent::MouseButtonPress,
                                                clickPos,
                                                Qt::LeftButton,
                                                Qt::MouseButton::NoButton,
                                                Qt::NoModifier);
        QCoreApplication::postEvent(eventsReciverWidget, press);
        // Some delay
        QTimer::singleShot(300, [clickPos, eventsReciverWidget]() {
            QMouseEvent *release = new QMouseEvent(QEvent::MouseButtonRelease,
                                                    clickPos,
                                                    Qt::LeftButton,
                                                    Qt::MouseButton::NoButton,
                                                    Qt::NoModifier);
            QCoreApplication::postEvent(eventsReciverWidget, release);
        }));
    }
    QWebEngineView webView = new QWebEngineView();
    // You need to find the first child widget of QWebEngineView. It can accept user input events.
    QWidget* eventsReciverWidget = nullptr;
    foreach(QObject* obj, webView->children())
    {
        QWidget* wgt = qobject_cast<QWidget*>(obj);
        if (wgt)
        {
            eventsReciverWidget = wgt;
            break;
        }
    }
    QPoint clickPos(100, 100);
    LeftMouseClick(eventsReciverWidget, clickPos);
    

    【讨论】:

      猜你喜欢
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多