首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模拟鼠标拖动事件PyQt5

模拟鼠标拖动事件PyQt5
EN

Stack Overflow用户
提问于 2021-06-02 19:13:27
回答 1查看 621关注 0票数 1

我正在制作一个机器人,autoClicks是一个QWebEngineView小部件。

我想在这个小部件上模拟一个mouseDrag。

ie:-

QPoint(100,100)上的

  • 按左键btn

QPoint(500,500)

  • 将鼠标移动到

  • 发布左侧小鼠btn

我试过这段代码,但不起作用:

代码语言:javascript
复制
def drag_from_to(browser_widget, x1, y1, x2, y2):

    for child in browser_widget.findChildren(QtWidgets.QWidget):
        if (child.metaObject().className() == "QtWebEngineCore::RenderWidgetHostViewQtDelegateWidget"):
            #I also tried sending the event to 'browser_widget' instead of 'child'
            event_press = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress, QtCore.QPoint(x1, y1),
                                            QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, )
            QtCore.QCoreApplication.postEvent(child, event_press)

            event_move = QtGui.QMouseEvent(QtCore.QEvent.MouseMove, QtCore.QPoint(x2, y2),
                                           QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, )
            QtCore.QCoreApplication.postEvent(child, event_move)

            event_release = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonRelease, QtCore.QPoint(x2, y2),
                                              QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, )
            QtCore.QCoreApplication.postEvent(child, event_release)
            return

编辑:我也尝试过QTest.mouseMove()..。它实际上移动鼠标光标..。但是我想让机器人在后台运行..。我希望用户能够在Bot运行时使用他的计算机

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-04 03:16:43

经过一些测试,我找到了解决办法:-

在发布一个新的QEvent.MouseMove之后,我必须暂停代码的执行至少1毫秒,我还必须顺序地将该事件发送到两点之间的每一个点,例如:将鼠标从QPoint(x1,y1)拖到QPoint(x2,y2)。

以下是工作代码:

代码语言:javascript
复制
def wait(ms): QTest.qWait(ms) #pauses the code without freezing the UI
def getIntEquidistantPoints(x1, y1, x2, y2):
    n = int(((((x2 - x1 )**2) + ((y2-y1)**2))**0.5))
    def lerp(v0, v1, i): return v0 + i * (v1 - v0)
    return [(int(x), int(y)) for x,y in [(lerp(x1,x2,1./n*i), lerp(y1,y2,1./n*i)) for i in range(n+1)]]
def drag_from_to(browser_widget, x1, y1, x2, y2):
    x1 = int(x1);y1 = int(y1);x2 = int(x2);y2 = int(y2)
    for child in browser_widget.findChildren(QtWidgets.QWidget):
        if (child.metaObject().className() == "QtWebEngineCore::RenderWidgetHostViewQtDelegateWidget"):
            event_press = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress, QtCore.QPoint(x1, y1),
                                            QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, )
            QtCore.QCoreApplication.postEvent(child, event_press)
            for p in getIntEquidistantPoints(x1, y1, x2, y2):
                QtCore.QCoreApplication.postEvent(child,
                                                  QMouseEvent(QEvent.MouseMove,
                                                              QtCore.QPoint(p[0], p[1]),
                                                              Qt.NoButton,
                                                              Qt.MouseButtons(Qt.LeftButton),
                                                              Qt.NoModifier)); wait(1)
            event_release = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonRelease, QtCore.QPoint(x2, y2),
                                              QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, )
            QtCore.QCoreApplication.postEvent(child, event_release)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67811160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档