I am trying to implement a sample code listed in the PySide2 documentation.
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:
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