QTreeView item overlaps
-
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?

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) -
Hi,
What if you put the pen width back to 1 before painting the path ?
-
Can you provide a minimal script so we can test your implementation in a similar fashion as you ?
-
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()) -
drawPathis not called because your if is wrong. You should see python screaming at you in your console. It should beif QStyle.State_HasFocus & option.state: -
@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 -
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)
