Qt.ItemDataRole.ForegroundRole for QtCore.QAbstractTableModel is not working with PySide6.7.2 (PySide6.2.4 is OK)
-
I am trying customed table cells by QtWidgets.QTableView and QtCore.QAbstractTableModel.
When I set Qt.ItemDataRole.ForegroundRole under venv pyside67 (pip install pyside6), the text in cells is all black(ignored ForegroundRole).
Then I tried same script under venv pyside62 (pip install 'pyside6<6.3,>=6.2'), the text in cells is customed as I wish.
Is there any probrem in PySide6.7.2?
Or do I miss something for PySide6.7.2?OS:Microsoft Windows 11 Pro
Python: 3.10.11script and venv infomation is below
# c.f. https://www.pythonguis.com/tutorials/pyqt6-qtableview-modelviews-numpy-pandas/ import sys from PySide6 import QtCore, QtGui, QtWidgets from PySide6.QtCore import Qt class TableModel(QtCore.QAbstractTableModel): def __init__(self, data): super(TableModel, self).__init__() self._data = data def data(self, index, role): if role == Qt.ItemDataRole.DisplayRole: return self._data[index.row()][index.column()] if role == Qt.ItemDataRole.ForegroundRole: if index.column()==2: return QtGui.QBrush('white') if role == Qt.ItemDataRole.BackgroundRole: if index.column()==2: return QtGui.QBrush('blue') def rowCount(self, index): return len(self._data) def columnCount(self, index): return len(self._data[0]) class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.table = QtWidgets.QTableView() data = [ [4, 9, 2], [1, 0, 0], [3, 5, 0], [3, 3, 2], [7, 8, 9], ] self.model = TableModel(data) self.table.setModel(self.model) self.setCentralWidget(self.table) app=QtWidgets.QApplication(sys.argv) window=MainWindow() window.show() app.exec_()
venv pyside67 (THIS IS NOT WORKING)
> pip list Package Version ------------------ ------- pip 24.1 PySide6 6.7.2 PySide6_Addons 6.7.2 PySide6_Essentials 6.7.2 setuptools 70.1.1 shiboken6 6.7.2 wheel 0.43.0 > python.exe -V Python 3.10.11 >
venv pyside62 (THIS IS OK)
> pip list Package Version ---------- ------- pip 24.1.2 PySide6 6.2.4 setuptools 70.3.0 shiboken6 6.2.4 wheel 0.43.0 > python.exe -V Python 3.10.11 >
-
Hi and welcome to devnet,
Try forcing the style to "windowsvista". The current Windows 11 style has some issues.
In any case, your code works fine with the same version of PySide6 on macOS.
-
Hi and welcome to devnet,
Try forcing the style to "windowsvista". The current Windows 11 style has some issues.
In any case, your code works fine with the same version of PySide6 on macOS.
@SGaist , thanks a lot.
app=QtWidgets.QApplication(sys.argv) app.setStyle('windowsvista')
The revised code(above) is working venv67 as well.
Some clients are expecting Win10 and Win11, so this is a big help.
-