Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2148

QStandartItem in QTreeView overlaps

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • Not Evaluated
    • None
    • 6.4.0
    • Other
    • None
    • Windows 11 Dev Build
      Python: 3.11.0
      PySide: 6.4.1
    • Windows

    Description

      I wrote my own delegator for QStandardItem. It doesn't work as it should at all. I've already asked in the forum, but they can't help me either. Example code below. I have attached a video that shows the problem.

       

      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 & 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())
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            crmaurei Cristian Maureira-Fredes
            firecube Ben Fässler
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes