Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Python/PyQt] How to make arrow in QTreeView::branch visible after adjusting selection background
Forum Update on Monday, May 27th 2025

[Python/PyQt] How to make arrow in QTreeView::branch visible after adjusting selection background

Scheduled Pinned Locked Moved Language Bindings
6 Posts 2 Posters 8.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    needhelp_gh
    wrote on 12 Jul 2013, 01:17 last edited by
    #1

    hi everyone,

    How can I make the arrow in QTreeView::branch visible after adjusting selection-background-color in stylesheet?

    Here is my code:
    @ #!/usr/bin/env python

    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
     
    class MainWindow(QMainWindow):
        def __init__(self, parent=None, **kwargs):
            QMainWindow.__init__(self, parent=None, **kwargs)
     
            self.setStyleSheet("""
               QTreeView::item {
                  background-color: rgb(49,49,49);
                  border-left: 1px solid rgb(40,40,40);
               }
     
              QTreeView::item::selected {
                 background-color: rgb(103,95,92);
                 color: rgb(40,40,40);
              }
     
               QTreeView QHeaderView:section {
                  background-color: rgb(44,44,44);
                  color: rgb(133,133,133);
               }
    
              QTreeView::branch {
                   selection-background-color: blue;
               }
    
     
           """)
     
            self.tree = QTreeWidget(self)
            self.tree.resize(300,300)
            self.parent = QTreeWidgetItem(self.tree, "test")
            self.child = QTreeWidgetItem(self.parent, ["test", "test", "test"])
     
    if __name__=="__main__":
        from sys import argv, exit
     
        a=QApplication(argv)
        m=MainWindow()
        m.resize(300,300)
        m.show()
        m.raise_()
        exit(a.exec_())
    

    @

    Thanks!! :)


    http://abstrusegoose.com/432

    1 Reply Last reply
    0
    • N Offline
      N Offline
      needhelp_gh
      wrote on 29 Jul 2013, 23:56 last edited by
      #2

      Any ideas? :(


      http://abstrusegoose.com/432

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jazzycamel
        wrote on 30 Jul 2013, 08:15 last edited by
        #3

        I'm not sure I understand the issue. When I run your code I see the arrow for the parent item whether selected or not. The child won't have an arrow because it has no children itself, though it will be indented so that space is available should it be required in the future. Are you getting a different result of have I misunderstood the query?

        For the avoidance of doubt:

        1. All my code samples (C++ or Python) are tested before posting
        2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
        1 Reply Last reply
        0
        • N Offline
          N Offline
          needhelp_gh
          wrote on 30 Jul 2013, 16:44 last edited by
          #4

          Jazzy,

          Here is what I mean,

          Before clicking a QTreeWidgetItem in a QTreeWidget:

          !http://oi42.tinypic.com/23vgtnl.jpg(beforeClick)!

          Clicking a QTreeWidgetItem: in a QTreeWidget

          !http://oi40.tinypic.com/15noqow.jpg(clickingItem)!

          I hope this is more clear.

          Thanks!!


          http://abstrusegoose.com/432

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jazzycamel
            wrote on 31 Jul 2013, 13:21 last edited by
            #5

            When using stylesheets and overriding the branch style you have to explicitly specify the (user supplied) images you would like to use for the drop indicators (as well as those for the interconnecting lines). The last two sections of the stylesheet in the following example demonstrate this:

            @
            from PyQt4.QtCore import *
            from PyQt4.QtGui import *

            class MainWindow(QMainWindow):
            def init(self, parent=None, **kwargs):
            QMainWindow.init(self, parent=None, **kwargs)

                self.setStyleSheet("""
                    QTreeView::item {
                        background-color: rgb(49,49,49);
                        border-left: 1px solid rgb(40,40,40);
                    }
            
                    QTreeView::item::selected {
                        background-color: rgb(103,95,92);
                        color: rgb(40,40,40);
                    }
            
                     QTreeView QHeaderView:section {
                        background-color: rgb(44,44,44);
                        color: rgb(133,133,133);
                    }
            
                    QTreeView::branch {
                        background: rgb(49,49,49);                
                    }
            
                    QTreeView::branch::closed::has-children {
                        image: url(branch-closed.png);
                    }            
            
                    QTreeView::branch::open::has-children {
                        image: url(branch-open.png);
                    }
            
                """)
            
                self.tree = QTreeWidget(self)
                self.tree.resize(300,300)
                self.parent = QTreeWidgetItem(self.tree, "test")
                self.child = QTreeWidgetItem(self.parent, ["test", "test", "test"])
            

            if name=="main":
            from sys import argv, exit

            a=QApplication(argv)
            m=MainWindow()
            m.resize(300,300)
            m.show()
            m.raise_()
            exit(a.exec_())
            

            @

            I borrowed the images from the documentation "here":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qtreeview which also details all the selectors for doing this.

            Hope this helps ;o)

            For the avoidance of doubt:

            1. All my code samples (C++ or Python) are tested before posting
            2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
            1 Reply Last reply
            0
            • N Offline
              N Offline
              needhelp_gh
              wrote on 5 Aug 2013, 23:38 last edited by
              #6

              I was looking for a solution without having to involve images.

              But I'll use this for my last resort. Thanks so much!!


              http://abstrusegoose.com/432

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved