Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QTreeView item overlaps
Qt 6.11 is out! See what's new in the release blog

QTreeView item overlaps

Scheduled Pinned Locked Moved Unsolved Qt for Python
10 Posts 2 Posters 1.5k Views 1 Watching
  • 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.
  • Fire CubeF Offline
    Fire CubeF Offline
    Fire Cube
    wrote on last edited by
    #1

    Hi guys I'm trying to write my own delegate. I managed that too. However, it looks like they overlap items because the border is cropped. Can anyone help me?
    3de1bcb7-3c04-43b1-aa15-4d5344be0e5a-image.png

    class StyledItemDelegate(QStyledItemDelegate):
        def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex):
            painter.setRenderHint(QPainter.Antialiasing)
    
            path = QPainterPath()
    
            path.addRoundedRect(option.rect, 5, 5) 
    
            painter.fillPath(path, index.data(BACKGROUND_ROLE))
    
            painter.setPen(QPen(QBrush("black"), 2))
    
            painter.setFont(QFont("Segoe UI", 9, QFont.Normal))
    
            rect = option.rect
            rect.setX(rect.x() + 5)
        
            painter.drawText(rect, index.data(DISPLAY_ROLE))
    
            if QStyle.State_HasFocus in option.state:
                painter.drawPath(path)
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What if you put the pen width back to 1 before painting the path ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Fire CubeF Offline
        Fire CubeF Offline
        Fire Cube
        wrote on last edited by
        #3

        Thank you for the answer.
        This happens:
        bc567ea5-f245-46bd-bbfa-5b6c00da4cc6-image.png

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you provide a minimal script so we can test your implementation in a similar fashion as you ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Fire CubeF Offline
            Fire CubeF Offline
            Fire Cube
            wrote on last edited by
            #5
            from PySide6.QtWidgets import QApplication, QMainWindow, QStyledItemDelegate, QStyle, QStyleOptionViewItem, QTreeView
            from PySide6.QtCore import QModelIndex
            from PySide6.QtGui import QBrush, QStandardItem, QStandardItemModel, QPainter, QPainterPath, QPen, QFont
            
            import sys
            
            DISPLAY_ROLE = 0
            BACKGROUND_ROLE = 8
            
            class StyledItemDelegate(QStyledItemDelegate):
                def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex):
                    # painter.setRenderHint(QPainter.Antialiasing)
            
                    path = QPainterPath()
            
                    path.addRoundedRect(option.rect, 5, 5) 
            
                    painter.fillPath(path, index.data(BACKGROUND_ROLE))
            
                    painter.setPen(QPen(QBrush("black"), 2))
            
                    painter.setFont(QFont("Segoe UI", 9, QFont.Normal))
            
                    rect = option.rect
                    rect.setX(rect.x() + 5)
                
                    painter.drawText(rect, index.data(DISPLAY_ROLE))
            
                    painter.setPen(QPen(QBrush("black"), 1))
                    if QStyle.State_HasFocus in option.state:
                        painter.drawPath(path)
            
            class MainWindow(QMainWindow):
                def __init__(self):
                    super().__init__()
            
                    treeview = QTreeView()
                    self.setCentralWidget(treeview)
            
                    model = QStandardItemModel()
                    treeview.setModel(model)
                    treeview.setItemDelegate(StyledItemDelegate())
                    treeview.header().setVisible(False)
            
                    _ = QStandardItem("my item")
                    model.appendRow(_)
                    model.setData(_.index(), QBrush("#ffffff"), role=BACKGROUND_ROLE)
            
                    _ = QStandardItem("my item")
                    model.appendRow(_)
                    model.setData(_.index(), QBrush("#ffffff"), role=BACKGROUND_ROLE)
            
            app = QApplication(sys.argv)
            
            window = MainWindow()
            window.show()
            
            sys.exit(app.exec())
            
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              drawPath is not called because your if is wrong. You should see python screaming at you in your console. It should be if QStyle.State_HasFocus & option.state:

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Fire CubeF Offline
                Fire CubeF Offline
                Fire Cube
                wrote on last edited by Fire Cube
                #7

                @SGaist said in QTreeView item overlaps:

                if QStyle.State_HasFocus & option.state:

                Changing this doesn't help.
                I use PySide6 6.4.1 with Python 3.11 on Windows 11.
                https://1drv.ms/v/s!AhT7GNW97XSLgYzFSvK9xtsRkMr1Rog?e=2g2hUX

                1 Reply Last reply
                0
                • Fire CubeF Offline
                  Fire CubeF Offline
                  Fire Cube
                  wrote on last edited by
                  #8

                  Can this be a bug in Qt?

                  1 Reply Last reply
                  0
                  • Fire CubeF Offline
                    Fire CubeF Offline
                    Fire Cube
                    wrote on last edited by
                    #9

                    Can I still get any help?
                    If I should try something or report it as a bug, please tell me.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      One thing you can do is to adjust the rect for the QPainterPath to ensure you paint in it.

                      path.addRoundedRect(option.rect.adjusted(1, 1, -1, -1), 5, 5)
                      

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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