没有 MousePress 的 PyQt4 MouseMove 事件

发布于 2024-12-11 04:41:43 字数 389 浏览 3 评论 0原文

我需要捕捉用户何时将鼠标移动到 GUI 上,而不是当他们按住鼠标按钮时(这会做不同的事情)。

我找不到任何方便的方法来做到这一点, 除了定期查找鼠标位置并检查其之前的位置... 这会很糟糕。

mouseMoveEvent 仅当鼠标移动且按下鼠标左键时才会被调用, 当然,除非小部件具有“鼠标跟踪”功能。鼠标跟踪对我来说不是一个选项,因为当移动鼠标并按下鼠标左键时,GUI 的行为必须有所不同。

有没有内置的方法可以做到这一点? (或者只是任何聪明的想法?)

例如: 有没有办法随时检查鼠标左键是否被按下?
或者可以应用于 QRect(坐标)的“鼠标悬停”事件?

非常感谢。


Windows 7 (32)
蟒蛇2.7
PyQt4

I need to catch when a User moves the mouse over the GUI, but not when they're holding down the mouse button (which would do something different).

I can't find any conveniant method to do this,
except to periodically find the mouse position and check it to it's previous position...
Which would suck.

The mouseMoveEvent is only called when the mouse is moved whilst the left mouse button is pressed,
unless ofcourse the widget has 'mouse tracking'. Mouse tracking is not an option for me, because the GUI must behave differently when the mouse is moved and the left mouse button is pressed.

Are there any inbuilt methods to do this?
(or just any clever ideas?)

eg:
Is there a way to check if the left mouse button is being pressed at any time?
Or a 'mouse hover' event that can be applied to a QRect (coordinates)?

Muchas gracias.


Windows 7 (32)
python 2.7
PyQt4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒浓于脸红 2024-12-18 04:41:43

最直接的方法是在 qApp 上安装事件过滤器:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        widget = QtGui.QWidget(self)
        layout = QtGui.QVBoxLayout(widget)
        self.edit = QtGui.QLineEdit(self)
        self.list = QtGui.QListWidget(self)
        layout.addWidget(self.edit)
        layout.addWidget(self.list)
        self.setCentralWidget(widget)

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.MouseMove:
            if event.buttons() == QtCore.Qt.NoButton:
                pos = event.pos()
                self.edit.setText('x: %d, y: %d' % (pos.x(), pos.y()))
            else:
                pass # do other stuff
        return QtGui.QMainWindow.eventFilter(self, source, event)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    win = Window()
    win.show()
    app.installEventFilter(win)
    sys.exit(app.exec_())

The most straightforward way to do this is to install an event filter on qApp:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        widget = QtGui.QWidget(self)
        layout = QtGui.QVBoxLayout(widget)
        self.edit = QtGui.QLineEdit(self)
        self.list = QtGui.QListWidget(self)
        layout.addWidget(self.edit)
        layout.addWidget(self.list)
        self.setCentralWidget(widget)

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.MouseMove:
            if event.buttons() == QtCore.Qt.NoButton:
                pos = event.pos()
                self.edit.setText('x: %d, y: %d' % (pos.x(), pos.y()))
            else:
                pass # do other stuff
        return QtGui.QMainWindow.eventFilter(self, source, event)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    win = Window()
    win.show()
    app.installEventFilter(win)
    sys.exit(app.exec_())
你对谁都笑 2024-12-18 04:41:43

正如人们所说,正确的方法似乎是在小部件上调用 setMouseTracking(True) 。我想补充的是,完成此操作后,您可以区分鼠标移动和鼠标拖动,如下所示:

def mouseMoveEvent(self, event):
    if event.buttons() == QtCore.Qt.NoButton:
        print "Simple mouse motion"
    elif event.buttons() == QtCore.Qt.LeftButton:
        print "Left click drag"
    elif event.buttons() == QtCore.Qt.RightButton:
        print "Right click drag"

As people have said, the correct approach seems to be to call setMouseTracking(True) on the widget. What I'd like to add is that, having done this, you can distinguish between a mouse motion and a mouse drag as follows:

def mouseMoveEvent(self, event):
    if event.buttons() == QtCore.Qt.NoButton:
        print "Simple mouse motion"
    elif event.buttons() == QtCore.Qt.LeftButton:
        print "Left click drag"
    elif event.buttons() == QtCore.Qt.RightButton:
        print "Right click drag"
水水月牙 2024-12-18 04:41:43

首先调用 setMouseTracking(True) 方法。然后 mouseMoveEvent 将在没有按下任何按钮的情况下被触发。

call setMouseTracking(True) method first. Then mouseMoveEvent will be fired without any button pressed.

離殇 2024-12-18 04:41:43

您似乎误解了 mouseTracking做。它只会导致 mouseMoveEvent 被触发,不会发生任何其他事情。换句话说,这正是您所需要的。

检查事件的 buttons() 以查看是否按下了任何按钮:

对于鼠标移动事件,这是按下的所有按钮。

It seems you've misunderstood what mouseTracking does. It only causes mouseMoveEvent to be fired, nothing else. In other words, it's exactly what you need.

Check the event's buttons() to see if any button was pressed:

For mouse move events, this is all buttons that are pressed down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文