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

  • Home
  • SEARCH
  • 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 6941881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:56:34+00:00 2026-05-27T12:56:34+00:00

I made a tree structure with QTreeWidget , and it works well. But I

  • 0

I made a tree structure with QTreeWidget, and it works well. But I have a problem with that. Commonly, with a tree structure, if I want to deselect all, I click on a blank area of the tree-widget. But QTreeWidget does not seem to support that (or I can’t find out how).

Could sub-classing or something else solve the problem? Determining a click on a blank area seems the key to the solution, but I can’t find a signal or anything.

  • 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-27T12:56:35+00:00Added an answer on May 27, 2026 at 12:56 pm

    The example code below will clear the selection (and current item) when clicking on a blank area, or when pressing Esc when the tree widget has the keyboard focus. It will work with any widget which inherits QAbstractItemView (not just QTreeWidget):

    class MyWidget(QTreeWidget):
        def keyPressEvent(self, event):
            if (event.key() == Qt.Key_Escape and
                event.modifiers() == Qt.NoModifier):
                self.selectionModel().clear()
            else:
                super(MyWidget, self).keyPressEvent(event)
    
        def mousePressEvent(self, event):
            if not self.indexAt(event.pos()).isValid():
                self.selectionModel().clear()
            super(MyWidget, self).mousePressEvent(event)
    

    To avoid subclassing, an event-filter can be used instead:

    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.widget = QTreeWidget()
            self.widget.installEventFilter(self)
            self.widget.viewport().installEventFilter(self)
            ...
    
        def eventFilter(self, source, event):
            if ((source is self.widget and
                 event.type() == QEvent.KeyPress and
                 event.key() == Qt.Key_Escape and
                 event.modifiers() == Qt.NoModifier) or
                (source is self.widget.viewport() and
                 event.type() == QEvent.MouseButtonPress and
                 not self.widget.indexAt(event.pos()).isValid())):
                self.widget.selectionModel().clear()
            return super(Window, self).eventFilter(source, event)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that builds up a tree structure made up of classes
I made a tree structure that only has a root. Then it's up to
I am having a tree data structure that can have different types of nodes
I want to have a map that permits a tree-like behaviour. I want to
I know tree is a well studied structure. I'm writing a program that randomly
I have a large Quad Tree structure, made from generic lists ( List<T> )
i have made an binary tree, and im trying to make an postorder traversal.
I have a flex project that is made of up several custom components that
I have a problem concerning good application design that has been keeping me up
I've never had any problem with the basic gem tree structure, namely bin (executables)

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.