使用大型数据集滚动时,PyQt QTableView会非常慢

San*_*era 7 python qtableview pyqt4

我有一个程序从csv文件加载配置文件并在表中显示数据.将pandas数据框加载到表中的速度很快,因为我使用了自定义模型实现QAbstractTableModel,但是QTableView小部件的大小调整非常慢.

我该怎么做才能使调整大小和滚动更顺畅?

San*_*era 7

好吧,我最后修改了我使用numpy制作的自定义表模型,现在它速度非常快.

使用此表格模型:

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 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
Run Code Online (Sandbox Code Playgroud)

  • `return self._data[index.row(),index.column()]` 对我来说根本不起作用。来自 [scipy.org](http://docs.scipy.org/doc/numpy/reference/ generated/numpy.ndarray.item.html) a.item(*args) 与 a[args] 非常相似,除了,返回标准 Python 标量,而不是数组标量。这对于加速对数组元素的访问以及使用 Python 的优化数学对数组元素进行算术运算非常有用。因此我使用了 `row_column = tuple([index.row(),index.column()])` `return self._data.item(row_column)` (2认同)