2012-02-25 8 views
4

QTabWidgetで中マウスのクリックを検出したいと思います。 QWidgetにマウスイベント関連のシグナルがあることを期待していましたが、私が見ているのはメソッドです。QTabWidgetのPyQtマウスイベント

QTabWidgetをサブクラス化してから、そのメソッドをオーバーライドして、必要な処理を行う必要がありますか、何か不足していますか?

答えて

7

あなたは、受信およびプレスを処理し、mousePressEventmouseReleaseEventを再定義し、QTabWidget.setTabBar()QTabWidgetQTabBarを交換するイベント、またはサブクラスQTabBarを解放する(QTabWidget.tabBar()によって返された)QTabBarのイベントフィルタをインストールすることができます。イベントフィルタ使用

  1. 例:

    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 
    
  2. をQTabBarサブクラスを使用して実施例:などのために多くのコードがある理由

    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 
    

(ケースで、あなたは不思議に思います単純なタスクでは、クリックはプレスイベントとして定義され、続いてほぼ同じ場所でリリースイベントが定義されるため、プレセンスのインデックスdタブはリリースされたタブと同じでなければなりません)。

+0

+1実際にプレスとリリースでクリック信号を複製する方法を示すための+1。古いタブインデックスと新しいタブインデックスを使用してクリックが有効かどうかを判断していない場合、一般的なアプローチでは古いポイントと新しいポイントの長さを使用して、許容範囲内でクリックをクリックするかどうかを判断します。 http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpoint.html#manhattanLength – jdi

+0

まさに私が望んでいた答えの種類。イベントフィルタを認識していませんでした。そのような詳細でよく説明された回答をくれてありがとう。 – Mathieson

+0

@マシソン:もしあなたが好きなら、彼の答えを受け入れるべきです。 – jdi