python - 捕获 QMainWindow 外部的鼠标位置(无需单击)

标签 python pyqt mouseevent qmainwindow

我尝试过:

        self.installEventFilter(self)

和:

        desktop= QApplication.desktop()
        desktop.installEventFilter(self)

与:

    def eventFilter(self, source, event):
        if event.type() == QEvent.MouseMove:
            print(event.pos())
        return QMainWindow.eventFilter(self, source, event)

在 QMainWindow 对象中,但没有任何结论。
你有什么想法吗?

最佳答案

鼠标事件最初由窗口管理器处理,然后将它们传递到屏幕该区域中的任何窗口。因此,如果该区域没有 Qt 窗口,您将不会收到任何事件(包括鼠标事件)。

但是,仍然可以通过轮询来跟踪光标位置:

from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    cursorMove = QtCore.pyqtSignal(object)

    def __init__(self):
        super(Window, self).__init__()
        self.cursorMove.connect(self.handleCursorMove)
        self.timer = QtCore.QTimer(self)
        self.timer.setInterval(50)
        self.timer.timeout.connect(self.pollCursor)
        self.timer.start()
        self.cursor = None

    def pollCursor(self):
        pos = QtGui.QCursor.pos()
        if pos != self.cursor:
            self.cursor = pos
            self.cursorMove.emit(pos)

    def handleCursorMove(self, pos):
        print(pos)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 500, 200, 200)
    window.show()
    sys.exit(app.exec_())

关于python - 捕获 QMainWindow 外部的鼠标位置(无需单击),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29399172/

相关文章:

python - QPushButton 切换连接似乎并未在开始时触发

python - 使用 native 对话框时 PyQt QFileDialog 不会关闭

python - 从 Flask 表单中删除搜索词

python - 寻找 PyQt4 嵌入式终端小部件

python - 想要安装六个,它给了我错误 "No module named ' 六'“

javascript - 在javascript中模拟鼠标点击时如何设置目标属性?

javascript - 数组长度在清空后不会增加,并且无法在 HTMLCollection 上添加鼠标事件

javascript - QML - 如何在 JavaScript 中修改 MouseArea onPressed 和 onReleased?

python - 在Python中解析字符串列表

python - 为什么 typing.Mapping 不是协议(protocol)?