QTabWidget的PyQt鼠标事件 [英] PyQt mouse events for QTabWidget

查看:425
本文介绍了QTabWidget的PyQt鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测QTabWidget上的鼠标中键.我原本希望在QWidget上有一个与鼠标事件相关的信号,但是我所看到的只是方法.

I want to detect middle mouse clicks on a QTabWidget. I was expecting there to be a mouse event related signal on QWidget, but all I am seeing are methods.

我是否需要继承QTabWidget的子类,然后重写所述方法才能执行我想要的操作,或者我遗漏了什么?

Do I need to subclass the QTabWidget and then override said methods in order to do what I want, or am I missing something?

推荐答案

您可以在QTabBar(由QTabWidget.tabBar()返回)上安装事件过滤器,以接收和处理新闻发布事件,或者子类重新定义mousePressEventmouseReleaseEvent并将QTabWidgetQTabBar替换为QTabWidget.setTabBar().

You can either install an event filter on the QTabBar (returned by QTabWidget.tabBar()) to receive and handle press and release events, or subclass QTabBar to redefine mousePressEvent and mouseReleaseEvent and replace the QTabBar of the QTabWidget with QTabWidget.setTabBar().

  1. 使用事件过滤器的示例:

  1. Example using the event filter:

class MainWindow(QMainWindow):
    def __init__(self):
        super(QMainWindow,self).__init__()
        self.tabWidget = QTabWidget(self)
        self.setCentralWidget(self.tabWidget)
        self.tabWidget.tabBar().installEventFilter(self)
        self.tabWidget.tabBar().previousMiddleIndex = -1           

    def eventFilter(self, object, event):
        if object == self.tabWidget.tabBar() and \
            event.type() in [QEvent.MouseButtonPress, 
                             QEvent.MouseButtonRelease] and \
            event.button() == Qt.MidButton: 
            tabIndex = object.tabAt(event.pos())
            if event.type() == QEvent.MouseButtonPress:
                object.previousMiddleIndex = tabIndex
            else:   
                if tabIndex != -1 and tabIndex == object.previousMiddleIndex:
                    self.onTabMiddleClick(tabIndex)                    
                object.previousMiddleIndex = -1                        
            return True               
        return False

    # function called with the index of the clicked Tab
    def onTabMiddleClick(self, index):
        pass

  • 使用QTabBar子类的示例:

  • Example using a QTabBar subclass:

    class TabBar(QTabBar):
        middleClicked = pyqtSignal(int)
    
        def __init__(self):
            super(QTabBar, self).__init__()
            self.previousMiddleIndex = -1
    
        def mousePressEvent(self, mouseEvent):
            if mouseEvent.button() == Qt.MidButton:
                self.previousIndex = self.tabAt(mouseEvent.pos())
            QTabBar.mousePressEvent(self, mouseEvent)
    
        def mouseReleaseEvent(self, mouseEvent):
            if mouseEvent.button() == Qt.MidButton and \
                self.previousIndex == self.tabAt(mouseEvent.pos()):
                self.middleClicked.emit(self.previousIndex)
            self.previousIndex = -1
            QTabBar.mouseReleaseEvent(self, mouseEvent)
    
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super(QMainWindow,self).__init__()
            self.tabWidget = QTabWidget(self)
            self.setCentralWidget(self.tabWidget)
    
            self.tabBar = TabBar()
            self.tabWidget.setTabBar(self.tabBar)
            self.tabBar.middleClicked.connect(self.onTabMiddleClick)
    
        # function called with the index of the clicked Tab
        def onTabMiddleClick(self, index):
            pass
    

  • (如果您想知道为什么如此简单的任务有这么多代码,将单击定义为按下事件,然后在大致相同的位置释放事件,因此,按下选项卡的索引必须是与已发布的标签相同.

    (In case you wonder why there is so much code for such a simple task, a click is defined as a press event followed by a release event at roughly the same spot, so the index of the pressed tab has to be the same as the released tab).

    这篇关于QTabWidget的PyQt鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆