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 8166359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:50:15+00:00 2026-06-06T19:50:15+00:00

Is there anyway to detect which mouse button was clicked from inside a QItemSelectionModel?

  • 0

Is there anyway to detect which mouse button was clicked from inside a QItemSelectionModel?

I want to block a right mouse click from changing the selection.

I am using this was a QTreeWidget, so if there is a way to mask the whole thing, that would be great, but the right-click is still used for context menus, so I didn’t pursue this avenue of thought.

Still trying things… I stumbled upon this but I haven’t been able to get the function to run: http://qt-project.org/faq/answer/how_to_prevent_right_mouse_click_selection_for_a_qtreewidget
This implies a simple override, but this didn’t work in Python

def mousePressEvent(self, mouse_event):
    super(MyTreeWidget, self).mousePressEvent(mouse_event)
    print "here %s" % event.type()
  • 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-06-06T19:50:17+00:00Added an answer on June 6, 2026 at 7:50 pm

    This feels like yet another workaround, but I got it working. In this example, the SelectionModel is also an event filter that is getting mouse click events from a QTreeWidget’s viewport()

    Also see:

    • What can block mousePressEvent or Event Filter Mouse Click Events?
    • QT QItemSelectionModel to ignore columns?

    (Hopefully I didn’t leave anything out as I hacked this down on-the-fly, and my real implementation is a little more complex, and uses a separate event filter.)

    from PyQt4.QtGui import QItemSelectionModel
    from PyQt4.QtCore import QEvent
    from PyQt4.QtCore import Qt
    
    # In the widget class ('tree' is the QTreeWidget)...
        # In __init___ ...
            self.selection_model = CustomSelectionModel(self.tree.model())
            self.tree.viewport().installEventFilter(self.selection_model)
    
    # In the selection model...
    class CustomSelectionModel(QItemSelectionModel):
        def __init__(self, model):
            super(CustomSelectionModel, self).__init__(model)
            self.is_rmb_pressed = False
    
        def eventFilter(self, event):
            if event.type() == QEvent.MouseButtonRelease:
                self.is_rmb_pressed = False
            elif event.type() == QEvent.MouseButtonPress:
                if event.button() == Qt.RightButton:
                    self.is_rmb_pressed = True
                else:
                    self.is_rmb_pressed = False
    
        def select(self, selection, selectionFlags):
            # Do nothing if the right mouse button is pressed
            if self.is_rmb_pressed:
                return
    
            # Fall through. Select as normal
            super(CustomSelectionModel, self).select(selection, selectionFlags)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway, in PHP, to detect someone's username? I want to track the
Is there any way to detect which windows XP theme is in use? I
Is there anyway I can detect via jQuery if a select form element is
Is there anyway that we can detect when the system/screen gets locked and notify
Is there any way to detect which keys are currently pressed using Tkinter? I
Is there any way i can detect for how long a button is press??
Is there any way in which we can detect status of incoming call in
Is there any way to detect the type from a given string input? Eg:
Is there any way to detect if a commercial is playing? I'm using player.observe(models.EVENT.CHANGE,
Is there any way to detect when an app is no longer active? That

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.