Код: Выделить всё
class TableModel(QAbstractTableModel):
def __init__(self, data):
super().__init__()
self._data = data
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
if index.isValid:
if role == Qt.ItemDataRole.DisplayRole:
value = self._data.iloc[index.row(), index.column()]
return str(value)
def rowCount(self, index):
return self._data.shape[0]
def columnCount(self, index):
return self._data.shape[1]
def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole):
# section is the index of the column/row.
if role == Qt.ItemDataRole.DisplayRole:
if orientation == Qt.Orientation.Horizontal:
return str(self._data.columns[section])
if orientation == Qt.Orientation.Vertical:
return str(self._data.index[section])
Код: Выделить всё
class DataWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("title")
self.table = QTableView()
self.data = pd.DataFrame()
self.model = TableModel(self.data)
self.table.setModel(self.model)
self.table.resize(800,600)
def display_table(self):
self.show()
Код: Выделить всё
Qt.ItemDataRole.DisplayRole
Код: Выделить всё
Qt.Orientation.Horizontal
Подробнее здесь: https://stackoverflow.com/questions/793 ... andas-data