2

The data in the table cells is not centering. It used to before but now all of a sudden it doesn't anymore, and I don't know why. I have not changed anything in the model class.

I have code similar to this MRE:

from PySide6 import QtWidgets as qtw
from PySide6 import QtCore as qtc


class Model(qtc.QAbstractTableModel):
    def __init__(self):
        super().__init__()
        self._data = [
            [1, 2],
            [3, 4],
            [5, 6]
        ]

    def data(self, index: qtc.QModelIndex, role: qtc.Qt.ItemDataRole = qtc.Qt.ItemDataRole.DisplayRole):
        if not index.isValid():
            return None
        
        if role == qtc.Qt.ItemDataRole.TextAlignmentRole:
            return qtc.Qt.AlignmentFlag.AlignCenter

        if role == qtc.Qt.ItemDataRole.DisplayRole:
            return self._data[index.row()][index.column()]

        return None    
    def rowCount(self, _):
        return len(self._data)
    
    def columnCount(self, _):
        return len(self._data[0])


class MainWindow(qtw.QMainWindow):
    def __init__(self):
        super().__init__()
        self._table = qtw.QTableView()
        self._table.setModel(Model())
        self.setCentralWidget(self._table)
        self.show()


app = qtw.QApplication()
mw = MainWindow()
app.exec()

Am I doing something wrong here?

オパラ
  • 317
  • 2
  • 10

1 Answers1

0

PySide6 has moved some enums around. Try this:

from PySide6.QtCore import Qt label.setAlignment(Qt.AlignmentFlag.AlignCenter)