python - PyQt5中有没有办法让鼠标事件完全忽略窗口?

标签 python pyqt pyqt5

我尝试过使用setAttribute(Qt.Qt.WA_TransparentForMouseEvents),但鼠标也无法穿透Qtwindow。

我想让鼠标事件穿透Qtwindow,就像我在位于Windows10桌面的Qtwindow上单击鼠标右键,然后它会触发win10上下文菜单。

最佳答案

透明窗口适合您的需求吗?

from PyQt5 import QtCore, QtWidgets, QtGui


class Overlay(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        self.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        layout = QtWidgets.QHBoxLayout(self)
        label = QtWidgets.QLabel('Transparent and propagating')
        label.setFont(QtGui.QFont('Arial', 26))
        label.setStyleSheet("background-color : white")
        layout.addWidget(label)
        self.show()


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    form = Overlay()
    app.exec_()

我试图找到一种将点击直接传输到桌面的方法。 closest related question给了我一些想法,但最终我无法让它工作,点击永远不会到达桌面。也许你仍然可以从中得到一些想法:

from PyQt5 import QtWidgets, QtGui
import win32api, win32con
from ctypes import windll


class Overlay(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        layout = QtWidgets.QHBoxLayout(self)
        label = QtWidgets.QLabel('Click to Desktop')
        label.setFont(QtGui.QFont('Arial', 26))
        label.setStyleSheet("background-color : white")
        layout.addWidget(label)
        # make window partially transparent to see where you are clicking
        self.setWindowOpacity(0.5)
        # get handle to desktop as described in linked question
        hProgman = windll.User32.FindWindowW("Progman", 0)
        hFolder = windll.User32.FindWindowExW(hProgman, 0, "SHELLDLL_DefView", 0)
        self.desktop = windll.User32.FindWindowExW(hFolder, 0, "SysListView32", 0)
        self.show()

    def mousePressEvent(self, event):
        # catch mouse event to route it to desktop
        x = event.globalPos().x()
        y = event.globalPos().y()
        lParam = win32api.MAKELONG(x, y)
        # left click on desktop (left button down + up, => should be replaced by event.button() pseudo switch case once working)
        windll.User32.SendInput(self.desktop, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
        windll.User32.SendInput(self.desktop, win32con.WM_LBUTTONUP, 0, lParam)
        # display position for debugging (position gets displayed, but nothing gets clicked)
        print(f'clicked on desktop at position {x} and {y}')


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    form = Overlay()
    app.exec_()

关于python - PyQt5中有没有办法让鼠标事件完全忽略窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65167075/

相关文章:

python - 在 pyqt 中将 sys.exit() 与 app.exec_ 一起使用

python - 将条件函数应用于 Pandas 中按天分组的数据的有效方法

Python hasattr 与 getattr

python - 如何使用 statsmodels 拟合 ARMAX 模型

python - PyQt 进度条 QThread 无法正常工作

python - vscode 智能感知与 PyQt4 配合使用太慢

python-3.x - 使用 pyqt5 读取特定值时更改 QLCDNumber 颜色

python - 如何对齐 QGraphicsTextItem 的网格?

python - 如何在pyqt qdock widget中添加图片

python - 如何在不初始化CUDA的情况下检查torch gpu兼容性?