I have a program that loads a profile from a csv file and displays the data in a table. The loading of a pandas data frame to the table is fast because I used a custom model implementation of QAbstractTableModel
, but the resizing of the QTableView widget is incredibly slow.
What can I do to make the resizing and scrolling smoother?
Well, I ended up modifying the custom table model I made to use numpy, and now it is blazing fast.
Updated 22-02-2020 Works as of Pandas 1.0.1:
Use this table model:
import numpy as np
class PandasModel(QtCore.QAbstractTableModel):
"""
Class to populate a table view with a pandas dataframe
"""
def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = np.array(data.values)
self._cols = data.columns
self.r, self.c = np.shape(self._data)
def rowCount(self, parent=None):
return self.r
def columnCount(self, parent=None):
return self.c
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data[index.row(),index.column()])
return None
def headerData(self, p_int, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return self._cols[p_int]
elif orientation == QtCore.Qt.Vertical:
return p_int
return None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With