Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7564575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:56:26+00:00 2026-05-30T13:56:26+00:00

I want to detect middle mouse clicks on a QTabWidget. I was expecting there

  • 0

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.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:56:26+00:00Added an answer on May 30, 2026 at 1:56 pm

    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. 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
      
    2. 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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to detect when my application is sent to the background. There are
I want to detect (preferably through an event) when any content is added, changed,
Possible Duplicate: Jquery: detect if middle or right mouse button is clicked, if so,
I want to detect the encoding of DBF automatically, but the structure of DBF
i want to detect processors fan speed is there any Java library to do
I want to detect if mytable view has been scrolled, I tried all touch
I want to detect Back Key event in my CustomView (e.g., EditText). In many
I want to detect clicks on any link on a page so that I
i want to detect to see if an element was dragged out of container.
I want to detect when an include in a jsp occurs in a web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.