全屏鼠标释放事件

发布于 2025-01-11 05:50:44 字数 897 浏览 0 评论 0原文

我有一个 pyqt 小部件,启动时会显示在所有窗口上。我需要它保持不关闭状态,直到用户决定这样做。是否可以在每次执行 mouseclick 时捕获鼠标释放事件,无论在何处完成:在 QtWidget 窗口内还是在 QtWidget 窗口外?

这是我使用的示例:

class Release_check(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
        self.canvas = iface.mapCanvas()
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
        self.grid = QGridLayout()
        self.grid.setSpacing(10)
        self.setGeometry(500, 500, 400, 100)

        self.text_out = QTextEdit()

        self.setLayout(self.grid)
        self.grid.addWidget(self.text_out, 0, 1, 1, 2)

        self.show()


    def mouseReleaseEvent(self, e):
        screen_coordinate = f"x:{e.x()}, y:{e.y()}"
        self.text_out.setText(screen_coordinate)
        super(Release_check, self).mouseReleaseEvent(e)


app = Release_check()

I have a pyqt widget that is shown over all windows when launched. I need it to keep unclosed until user decides to. Is that possible to catch mouse release event every time mouseclick performed no matter where it was done: inside QtWidget window or outside of it?

Here is a sample I use:

class Release_check(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
        self.canvas = iface.mapCanvas()
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
        self.grid = QGridLayout()
        self.grid.setSpacing(10)
        self.setGeometry(500, 500, 400, 100)

        self.text_out = QTextEdit()

        self.setLayout(self.grid)
        self.grid.addWidget(self.text_out, 0, 1, 1, 2)

        self.show()


    def mouseReleaseEvent(self, e):
        screen_coordinate = f"x:{e.x()}, y:{e.y()}"
        self.text_out.setText(screen_coordinate)
        super(Release_check, self).mouseReleaseEvent(e)


app = Release_check()

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

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

发布评论

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

评论(1

永言不败 2025-01-18 05:50:44

Qt 仅检测小部件内部的点击,如果您想检测小部件外部,则必须使用另一个使用操作系统资源来监视操作系统事件的库,例如 pyinput

import sys
from pynput import mouse
from PyQt5 import QtCore, QtGui, QtWidgets


class ButtonReleaseManager(QtCore.QObject):
    released = QtCore.pyqtSignal(int, int)

    def __init__(self, parent=None):
        super().__init__(parent)
        self._listener = mouse.Listener(on_click=self._handle_click)
        self._listener.start()

    def _handle_click(self, x, y, button, pressed):
        if not pressed:
            self.released.emit(x, y)


class Release_check(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.manager = ButtonReleaseManager()
        self.manager.released.connect(self.show_position)

    def initUI(self):
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
        self.setGeometry(500, 500, 400, 100)

        self.text_out = QtWidgets.QTextEdit()

        grid = QtWidgets.QGridLayout(self)
        grid.setSpacing(10)
        grid.addWidget(self.text_out, 0, 1, 1, 2)

    @QtCore.pyqtSlot(int, int)
    def show_position(self, x, y):
        screen_coordinate = f"x:{x}, y:{x}"
        self.text_out.setText(screen_coordinate)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Release_check()
    w.show()
    sys.exit(app.exec_())

Qt only detects the click inside the widget, if you want to detect outside the widgets then you must use another library that uses OS resources to monitor OS events such as pyinput:

import sys
from pynput import mouse
from PyQt5 import QtCore, QtGui, QtWidgets


class ButtonReleaseManager(QtCore.QObject):
    released = QtCore.pyqtSignal(int, int)

    def __init__(self, parent=None):
        super().__init__(parent)
        self._listener = mouse.Listener(on_click=self._handle_click)
        self._listener.start()

    def _handle_click(self, x, y, button, pressed):
        if not pressed:
            self.released.emit(x, y)


class Release_check(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.manager = ButtonReleaseManager()
        self.manager.released.connect(self.show_position)

    def initUI(self):
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
        self.setGeometry(500, 500, 400, 100)

        self.text_out = QtWidgets.QTextEdit()

        grid = QtWidgets.QGridLayout(self)
        grid.setSpacing(10)
        grid.addWidget(self.text_out, 0, 1, 1, 2)

    @QtCore.pyqtSlot(int, int)
    def show_position(self, x, y):
        screen_coordinate = f"x:{x}, y:{x}"
        self.text_out.setText(screen_coordinate)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Release_check()
    w.show()
    sys.exit(app.exec_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文