Qt detecting mouse click on titleBar (Windows)

后端 未结 2 1940
你的背包
你的背包 2021-01-28 08:47

event or mousePressEvent functions works for inside of a widget but I want to catch when clicked on a titleBar (upper part of menuBar, contains close button etc.)

How ca

2条回答
  •  鱼传尺愫
    2021-01-28 09:15

    Here's the PyQt implementation.

    from  PyQt5.QtWidgets import *
    from  PyQt5.QtCore import *
    
    import ctypes.wintypes
    import logging
    from win32con import *
    import win32api
    
    class W(QTabWidget):
        def nativeEvent(self, eventType, message):
            click_menu = QMenu(self)
            click_menu.addAction("Yay")
    
            try:
                msg = ctypes.wintypes.MSG.from_address(message.__int__())
            except:
                logging.error("", exc_info=True)
            if eventType == "windows_generic_MSG":
                if msg.message == WM_NCLBUTTONDOWN:
                    mouse_x = win32api.LOWORD(msg.lParam)
                    mouse_y = win32api.HIWORD(msg.lParam)
                    frame = self.frameGeometry()
                    content = self.geometry()
                    print(mouse_x, mouse_y, frame, content)
                    if mouse_y < content.y() and mouse_y >= frame.y():
    
                        click_menu.exec_(QPoint(mouse_x, mouse_y))
    
            return False, 0
    
    app = QApplication([])
    
    w = W()
    w.resize(1000,100)
    w.move(0,0)
    w.show()
    app.exec_()
    

    One thing I can't figure out about this, what happens to my menu if I click the titlebar again without clicking the menu. The menu disappears, which is then allows me to move the titlebar when clicking - which is good...but I can't reliably bring the menu back up

提交回复
热议问题