[PyQt] PyQt : cannot get native window

Zhao Lee redstone-cold at 163.com
Wed Jan 17 14:37:31 GMT 2018


I want to get the native window under the cursor after a mouse click , so as to hook the mousePressEvent of that window , my code as following
from PyQt5.QtCoreimport*from PyQt5.QtGuiimport*from PyQt5.QtWidgetsimport*import mouseimport win32guiclassNativeWindow(QWindow):

    def __init__(self, parent):
        super().__init__(parent)

    def mousePressEvent(self, mouseEvent):
        print('mouseEvent')

    def nativeEvent(self, eventType, message, result):
        print(eventType, message, result)classEngkuDict(QSystemTrayIcon):

    def __init__(self, parent=None):
        super().__init__(parent)

        self.bindSlot()

    def bindSlot(self):
        mouse.on_click(self.mouseClicked)

    def mouseClicked(self):
        currentMousePosition = mouse.get_position()
        windowHandle = win32gui.WindowFromPoint(currentMousePosition)
        print(windowHandle)
        n =NativeWindow(QWindow.fromWinId(windowHandle))
        print(n.title())if __name__ =="__main__":
    import sys
    app =QApplication(sys.argv)
    trayIcon =EngkuDict()
    trayIcon.show()
    sys.exit(app.exec_())

While running, the mousePressEvent was never called, and I could not get the window title using n.title() either , so what's wrong ? BTW, I have used third party modules mouse and pywin32.






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180117/963dd067/attachment-0001.html>


More information about the PyQt mailing list