89 lines
2.4 KiB
Python
89 lines
2.4 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
@author: Taar
|
|
"""
|
|
|
|
#conversion of https://github.com/openwebos/qt/tree/master/examples/tutorials/modelview/5_edit
|
|
|
|
#I work with ints for values instead of strings (less cumbersome to add)
|
|
|
|
import sys
|
|
import pandas as pd
|
|
|
|
from PySide2.QtWidgets import *
|
|
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
|
|
QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
|
|
QPixmap, QRadialGradient, QStandardItem, QStandardItemModel)
|
|
from PySide2.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
|
|
QAbstractTableModel, QObject, QPoint, QRect, QSize, QTime,
|
|
QUrl, Qt, QThread, Signal, QTimer)
|
|
|
|
class TableModel(QAbstractTableModel):
|
|
|
|
def __init__(self, data):
|
|
super(TableModel, self).__init__()
|
|
self._data = data
|
|
|
|
def data(self, index, role):
|
|
if role == Qt.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):
|
|
# section is the index of the column/row.
|
|
if role == Qt.DisplayRole:
|
|
if orientation == Qt.Horizontal:
|
|
return str(self._data.columns[section])
|
|
|
|
if orientation == Qt.Vertical:
|
|
return str(self._data.index[section])
|
|
|
|
class MainWindow(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
self.table = QTableView()
|
|
|
|
data = pd.DataFrame([
|
|
[1, 9, 2],
|
|
[1, 0, -1],
|
|
[3, 5, 2],
|
|
[3, 3, 2],
|
|
[5, 8, 9],
|
|
],
|
|
columns = ['A', 'B', 'C'],
|
|
index=['Row 1', 'Row 2', 'Row 3', 'Row 4', 'Row 5'])
|
|
|
|
self.model = TableModel(data)
|
|
self.table.setModel(self.model)
|
|
|
|
data = pd.DataFrame([
|
|
[1, 5, 5, 9],
|
|
[1, 5, 5, 9],
|
|
[1, 2, 1, 3],
|
|
[1, 1, 0, -1],
|
|
[1, 5, 5, 2],
|
|
[1, 5, 5, 8],
|
|
[1, 5, 5, 9],
|
|
],
|
|
columns = ['Aa', 'Bb', 'Cc', 'dd'],
|
|
index=['Row 1', 'Row 3', 'Row 4', 'Row 4', 'Row 5', '6', '7'])
|
|
|
|
self.model = TableModel(data)
|
|
self.table.setModel(self.model)
|
|
self.setCentralWidget(self.table)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app=QApplication(sys.argv)
|
|
window=MainWindow()
|
|
window.show()
|
|
app.exec_()
|