-3

I am trying to implement a sample code listed in the PySide2 documentation.

https://doc.qt.io/qtforpython-5.12/PySide2/QtWidgets/QAbstractItemDelegate.html#PySide2.QtWidgets.QAbstractItemDelegate

import sys
import os

from PySide2.QtWidgets import (
    QApplication,
    QMainWindow,
    QTableView,
    QVBoxLayout,
    QWidget,
    QStyleOptionProgressBar,
    QStyledItemDelegate,
    QStyle
)

from PySide2.QtCore import QAbstractTableModel, QModelIndex, QSortFilterProxyModel
from PySide2.QtGui import Qt

data = [
    ['123', '456', '789'],
    ['456', '123', '789'],
    ['789', '789', '123'],
]

class WidgetDelegate(QStyledItemDelegate):
    def __init__(self):
        super(WidgetDelegate, self).__init__()

    def paint(self, painter, option, index):
        if index.column() == 2:
            progress = 50
            self.progressBarOption = QStyleOptionProgressBar()
            self.progressBarOption.rect = option.rect
            self.progressBarOption.minimum = 0
            self.progressBarOption.minimum = 100
            QApplication.style().drawControl(QStyle.CE_ProgressBar, self.progressBarOption, painter)
        
        else:
            QStyledItemDelegate.paint(self, painter, option, index)


class TableModel(QAbstractTableModel):
    headerLabels = ['Header1', 'Header2', 'Header3']
    def __init__(self, input_data = None):
        super(TableModel, self).__init__()
        self.tableData = input_data
        if self.tableData is None:
            self.tableData = list()

    def rowCount(self, parent = QModelIndex):
        try:
            row_count = len(self.tableData)
            return row_count
        except:
            return 0

    def columnCount(self, parent = QModelIndex):
        try:
            column_count = len(self.tableData[0])
            return column_count
        except:
            return 0

    def headerData(self, section, orientation, role=Qt.DisplayRole):
        if role == Qt.DisplayRole and orientation == Qt.Horizontal:
            return self.headerLabels[section]

        return QAbstractTableModel.headerData(self, section, orientation, role)

    def data(self, index, role = Qt.DisplayRole):
        if role == Qt.DisplayRole or role == Qt.EditRole:
            return self.tableData[index.row()][index.column()]
        

class TableView(QTableView):
    def __init__(self):
        super(TableView, self).__init__()
        self.setSelectionBehavior(self.SelectRows)


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.tableView = TableView()
        self.delegate = WidgetDelegate()
        self.tableView.setItemDelegate(self.delegate)
        self.initUi()
        self.tableModel = TableModel(data)
        self.tableView.setModel(self.tableModel)

        
    def initUi(self):
        #----- Init UI -----
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.tableView)
        mainWidget = QWidget()
        mainWidget.setLayout(mainLayout)
        self.setCentralWidget(mainWidget)
        self.resize(650, 350)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

Instead of ProgressBars in the last column I am getting a broken layout: Layout screenshot:
Layout screenshot

Update:

It looks like a bug in PySide2 and PySide6 for MacOS. I've submitted a bug report: https://bugreports.qt.io/browse/PYSIDE-1464

  • It looks like a bug, what version of PySide2 do you use? The only drawback I see is that you have to change `self.progressBarOption` for `progressBarOption` – eyllanesc Dec 27 '20 at 14:08
  • I am using 5.15.2 version of PySide2 The example is somehow mix of python and C++, so I decided to state the variable as a class method, but without 'self' it works exactly similar. – Nastya Medvedeva Dec 27 '20 at 14:22
  • So it is a Qt bug in MacOS, I have tested your code in Linux and it works correctly – eyllanesc Dec 27 '20 at 14:25

1 Answers1

0

Thank you for help eyllanesc. It looks like a bug in PySide2 and PySide6 as well as PyQt5 for MacOS. I've submitted a bug report: https://bugreports.qt.io/browse/PYSIDE-1464

As of now I do not have any working way to implement QProgressBar delegate.