Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt QTableView prohibitively slow when scrolling with large data sets

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?

like image 926
Santi Peñate-Vera Avatar asked Jul 23 '15 13:07

Santi Peñate-Vera


1 Answers

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
like image 70
Santi Peñate-Vera Avatar answered Oct 12 '22 14:10

Santi Peñate-Vera