Chciałbym wyświetlić ramkę danych pandy w tabeli PyQt. Poczyniłem pewne postępy w tym zakresie, ale nie byłem w stanie prawidłowo wyprowadzić klasy modelu tabeli. Jakakolwiek pomoc w tej sprawie byłaby bardzo doceniana.PyQt - Implementacja QAbstractTableModel do wyświetlania w QTableView
** Uwaga pełny przykład kodu here **
walczę Wygenerowanie odpowiedniego QtCore.QAbstractTableModel klasy pochodnej. Kontynuując poprzednie pytanie dotyczące QItemDelegates, próbuję wygenerować model tabeli z Pandas DataFrame, aby wstawić prawdziwe dane. Mam działający kod przykładowy here, ale jeśli zamieniam mój TableModel na TableModel2 w klasie Widget (ln 152), nie mogę wyświetlić tabeli.
class TableModel2(QtCore.QAbstractTableModel):
def __init__(self, parent=None, *args):
super(TableModel2, self).__init__()
#QtCore.QAbstractTableModel.__init__(self, parent, *args)
self.datatable = None
self.headerdata = None
self.dataFrame = None
self.model = QtGui.QStandardItemModel(self)
def update(self, dataIn):
print 'Updating Model'
self.datatable = dataIn
print 'Datatable : {0}'.format(self.datatable)
headers = dataIn.columns.values
header_items = [
str(field)
for field in headers
]
self.headerdata = header_items
print 'Headers'
print self.headerdata
for i in range(len(dataIn.index.values)):
for j in range(len(dataIn.columns.values)):
#self.datatable.setItem(i,j,QtGui.QTableWidgetItem(str(df.iget_value(i, j))))
self.model.setItem(i,j,QtGui.QStandardItem(str(dataIn.iget_value(i, j))))
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.datatable.index)
def columnCount(self, parent=QtCore.QModelIndex()):
return len(self.datatable.columns.values)
def data(self, index, role=QtCore.Qt.DisplayRole):
if not index.isValid():
return QtCore.QVariant()
elif role != QtCore.Qt.DisplayRole:
return QtCore.QVariant()
#return QtCore.QVariant(self.model.data(index))
return QtCore.QVariant(self.model.data(index))
def headerData(self, col, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return QtCore.QVariant()
return QtCore.QVariant(self.headerdata[col])
def setData(self, index, value, role=QtCore.Qt.DisplayRole):
print "setData", index.row(), index.column(), value
def flags(self, index):
if (index.column() == 0):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled
else:
return QtCore.Qt.ItemIsEnabled
Ja próbuje stworzyć model, a następnie dodać go do widoku, tak:
class Widget(QtGui.QWidget):
"""
A simple test widget to contain and own the model and table.
"""
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
l=QtGui.QVBoxLayout(self)
cdf = self.get_data_frame()
self._tm=TableModel(self)
self._tm.update(cdf)
self._tv=TableView(self)
self._tv.setModel(self._tm)
for row in range(0, self._tm.rowCount()):
self._tv.openPersistentEditor(self._tm.index(row, 0))
l.addWidget(self._tv)
def get_data_frame(self):
df = pd.DataFrame({'Name':['a','b','c','d'],
'First':[2.3,5.4,3.1,7.7], 'Last':[23.4,11.2,65.3,88.8], 'Class':[1,1,2,1], 'Valid':[True, True, True, False]})
return df
Dziękujemy za uwagę!
Uwaga: edycja 2 Dołączyłem QStandardItemModel do TableModel2. Usunięto również funkcję dataFrameToQtTable po komentarzu @ mata. Jest coraz bliżej, ale wciąż nie działa.
Pandy jest przykładem tabeli Qt w repo . https://github.com/pydata/pandas/commits/master/pandas/sandbox/qtpandas.py –