[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    PyQt 6.7.0 QStyle CE_ItemViewItem bug on Windows 11
From:       Benjamin Griffiths <ben () familygriffiths ! me ! uk>
Date:       2024-05-14 15:04:47
Message-ID: 110451779.186396.1715699087369 () email ! ionos ! co ! uk
[Download RAW message or body]

[Attachment #2 (unknown)]

<!doctype html>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 </head>
 <body>
  <div class="default-style">
    Hi there,
  </div>
  <div class="default-style">
    &nbsp;
  </div>
  <div class="default-style">
    After upgrading from PyQt 6.6.1 to 6.7.0, my application is crashing silently on \
Windows 11, it works fine on Windows 10.  </div>
  <div class="default-style">
    &nbsp;
  </div>
  <div class="default-style">
    I have narrowed it down to the following line in a QStyleItemDelegate:&nbsp;
  </div>
  <div class="default-style">
    &nbsp;
  </div>
  <div class="default-style">
   <div> <strong>style.drawControl(QStyle.ControlElement.CE_ItemViewItem, options, \
painter)</strong>  </div>
   <div class="default-style">
     &nbsp;
   </div>
   <div class="default-style">
     If I change <strong>CE_ItemViewItem</strong> to something else, for example \
<strong>CE_MenuItem</strong>, the application does not crash.  </div>
  </div>
  <div class="default-style">
    &nbsp;
  </div>
  <div class="default-style">
    I have attached a minimal reproducable example that uses a QStandardModel and \
adds QStandardItem's to a QListView with a custom QStyledItemDelegate. Line 31 is \
what I believe causes the crash. I am using Python 3.10.11.  </div>
  <div class="default-style">
    &nbsp;
  </div>
  <div class="default-style">
    Any support would be greatly appreciated.
  </div>
  <div class="default-style">
    &nbsp;
  </div>
  <div class="default-style">
    Many thanks,
  </div>
  <div class="default-style">
    &nbsp;
  </div>
  <div class="default-style">
    Ben
  </div>
 </body>
</html>


["qstandardmodel_test.py" (text/x-python)]

import sys

from PyQt6.QtCore import QModelIndex, Qt
from PyQt6.QtGui import QPainter, QStandardItem, QStandardItemModel
from PyQt6.QtWidgets import (
    QApplication,
    QListView,
    QMainWindow,
    QPushButton,
    QStyle,
    QStyledItemDelegate,
    QStyleOptionViewItem,
    QVBoxLayout,
    QWidget,
)


class Delegate(QStyledItemDelegate):
    def __init__(self):
        QStyledItemDelegate.__init__(self)

    def paint(
        self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex
    ):
        options = QStyleOptionViewItem(option)
        self.initStyleOption(options, index)
        if options.widget:
            style = options.widget.style()
        else:
            style = QApplication.style()
        style.drawControl(QStyle.ControlElement.CE_ItemViewItem, options, painter)

        text = index.data(Qt.ItemDataRole.UserRole)
        rect = option.rect
        font = painter.font()
        font.setPixelSize(16)
        font.setBold(True)
        painter.setFont(font)
        painter.drawText(
            rect,
            Qt.AlignmentFlag.AlignLeft,
            text,
        )


class MainWindow(QMainWindow):
    def __init__(self) -> None:
        QMainWindow.__init__(self)
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        self.main_layout = QVBoxLayout(self.central_widget)

        self.model = QStandardItemModel()
        self.view = QListView()
        self.view.setItemDelegate(Delegate())
        self.view.setModel(self.model)
        self.main_layout.addWidget(self.view)

        self.add_item_btn = QPushButton("Add Item")
        self.add_item_btn.clicked.connect(self.add_item)
        self.main_layout.addWidget(self.add_item_btn)

        self.show()

    def add_item(self) -> None:
        item = QStandardItem()
        item.setData(f"Item {self.model.rowCount()}", Qt.ItemDataRole.UserRole)
        self.model.appendRow([item])


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    sys.exit(app.exec())


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic