diff options
Diffstat (limited to 'sources/pyside6/doc/tutorials/datavisualize/datavisualize5/table_model.py')
-rw-r--r-- | sources/pyside6/doc/tutorials/datavisualize/datavisualize5/table_model.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/sources/pyside6/doc/tutorials/datavisualize/datavisualize5/table_model.py b/sources/pyside6/doc/tutorials/datavisualize/datavisualize5/table_model.py new file mode 100644 index 000000000..08eeeeed6 --- /dev/null +++ b/sources/pyside6/doc/tutorials/datavisualize/datavisualize5/table_model.py @@ -0,0 +1,51 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +from PySide6.QtCore import Qt, QAbstractTableModel, QModelIndex +from PySide6.QtGui import QColor + + +class CustomTableModel(QAbstractTableModel): + def __init__(self, data=None): + QAbstractTableModel.__init__(self) + self.load_data(data) + + def load_data(self, data): + self.input_dates = data[0].values + self.input_magnitudes = data[1].values + + self.column_count = 2 + self.row_count = len(self.input_magnitudes) + + def rowCount(self, parent=QModelIndex()): + return self.row_count + + def columnCount(self, parent=QModelIndex()): + return self.column_count + + def headerData(self, section, orientation, role): + if role != Qt.DisplayRole: + return None + if orientation == Qt.Horizontal: + return ("Date", "Magnitude")[section] + else: + return f"{section}" + + def data(self, index, role=Qt.DisplayRole): + column = index.column() + row = index.row() + + if role == Qt.DisplayRole: + if column == 0: + date = self.input_dates[row].toPython() + return str(date)[:-3] + elif column == 1: + magnitude = self.input_magnitudes[row] + return f"{magnitude:.2f}" + elif role == Qt.BackgroundRole: + return QColor(Qt.white) + elif role == Qt.TextAlignmentRole: + return Qt.AlignRight + + return None + |