简体   繁体   中英

PyQt5 event.button() - Middle mouse to close tab prevents tabs being selected on OS X

I'm porting over a Python program written for Windows to Mac, and I'm experiencing issues with the Qt.MidButton function.

class MyTabBar(QTabBar):
    def __init__(self):
        super(MyTabBar, self).__init__()

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.MidButton:
            self.tabCloseRequested.emit(self.tabAt(event.pos()))

Tabs are drawn up into the Tab Bar, and the mouseReleaseEvent works as intended on OSX (Closing the tab when middle mouse clicked) however it doesn't allow the user to switch between tabs with a left click. If the close function is disabled, the user can click between tabs.

The problem is caused by overriding the mouseReleaseEvent method and not invoking super is eliminating the default behavior. The solution is to invoke super:

class MyTabBar(QTabBar):
    def mouseReleaseEvent(self, event):
        if event.button() == Qt.MidButton:
            self.tabCloseRequested.emit(self.tabAt(event.pos()))
        super(MyTabBar, self).mouseReleaseEvent(event)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM