How to make click-through windows PyQt

2019-04-11 15:51发布

问题:

I would like to make a window in PyQt that you can click through; ie click on a window and the click is passed through so you can interact with whatever is behind it, while the window remains on top. An example of the effect I am trying to achieve is like the notifications on Ubuntu which appear in the top-right hand corner by default, which you can click through.

I would like to be able to do this in PyQt ideally; if not, my platform is linux but windows solutions are also welcome!

Cheers for the help in advance! I've been giving this a bit of thought and research, it would be great to be able to do this.

EDIT: I am trying to make a window you can use like tracing paper for the window behind

回答1:

Here is a solution on Windows using PyQt4.

You need to override the eventFilter in the Front widget (on Windows it is winEvent) and then forward the events to the Back window.

I'm not completely sure, but there must be a similar approach that can be used on other platforms (instead of winEvent, maybe x11Event?)

Good luck!

from PyQt4 import QtCore, QtGui
import win32api, win32con, win32gui, win32ui

class Front(QtGui.QPushButton):
    def __init__(self,text="",whndl=None):
        super(Front,self).__init__(text)
        self.pycwnd = win32ui.CreateWindowFromHandle(whndl)

    # install an event filter for Windows' messages. Forward messages to 
    # the other HWND
    def winEvent(self,MSG):

        # forward Left button down message to the other window.  Not sure 
        # what you want to do exactly, so I'm only showing a left button click.  You could 
        if MSG.message == win32con.WM_LBUTTONDOWN or \
           MSG.message == win32con.WM_LBUTTONUP:

            print "left click in front window"
            self.pycwnd.SendMessage(MSG.message, MSG.wParam, MSG.lParam)
            return True, 0 # tells Qt to ignore the message

        return super(Front,self).winEvent(MSG)

class Back(QtGui.QPushButton):
    def __init__(self,text=""):
        super(Back,self).__init__(text)
        self.clicked.connect(self.onClick)

    def onClick(self):
        print 'back has been clicked'

def main():
    a = QtGui.QApplication([])

    back = Back("I'm in back...")
    back.setWindowTitle("I'm in back...")
    back.show()

    # Get the HWND of the window in back (You need to use the exact title of that window)
    whndl = win32gui.FindWindowEx(0, 0, None, "I'm in back...")

    # I'm just making the front button bigger so that it is obvious it is in front ...
    front = Front(text="*____________________________*",whndl=whndl)
    front.setWindowOpacity(0.8)
    front.show()    

    a.exec_()

if __name__ == "__main__":
    main()