PyQt5在带有 pandas 的QTableView上极其缓慢地滚动 [英] PyQt5 Extremely Slow Scrolling on QTableView with pandas
问题描述
我正在使用QTableView在PyQt5 GUI内创建一个表.我从熊猫数据框中有35行和5列.滚动和排序表非常慢(几秒钟).
I am creating a table inside of a PyQt5 GUI using QTableView. I have 35 rows and 5 columns from a pandas dataframe. Scrolling and sorting the table is extremely slow (on the order of seconds).
我已经在寻找解决方案,但是大多数人在填充表格时遇到了麻烦.有人建议使用numpy数组,但我看不到性能有任何提高.
I have already looked for solutions, but most people were having trouble with populating the table. One person suggested using a numpy array, but I did not see any increase in performance.
这是我的代码:
def create_table(dataframe):
table = QTableView()
tm = TableModel(dataframe)
table.setModel(tm)
table.setSelectionBehavior(QAbstractItemView.SelectRows)
table.resizeColumnsToContents()
table.resizeRowsToContents()
table.setSortingEnabled(True)
return table
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = data
def rowCount(self, parent=None):
return len(self._data.values)
def columnCount(self, parent=None):
return self._data.columns.size
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data.values[index.row()][index.column()])
return None
def headerData(self, rowcol, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return self._data.columns[rowcol]
if orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
return self._data.index[rowcol]
return None
def flags(self, index):
flags = super(self.__class__, self).flags(index)
flags |= QtCore.Qt.ItemIsEditable
flags |= QtCore.Qt.ItemIsSelectable
flags |= QtCore.Qt.ItemIsEnabled
flags |= QtCore.Qt.ItemIsDragEnabled
flags |= QtCore.Qt.ItemIsDropEnabled
return flags
def sort(self, Ncol, order):
"""Sort table by given column number.
"""
try:
self.layoutAboutToBeChanged.emit()
self._data = self._data.sort_values(self._data.columns[Ncol], ascending=not order)
self.layoutChanged.emit()
except Exception as e:
print(e)
table = create_table(dataframe)
我在这里找到一个问题在其他组件上使用QTableView缓慢滚动在用户遇到类似问题时,他/她发现"QTableView正在刷新窗口的每个滚动/外观上的项目,这显然是问题的根源."但是我不知道我的桌子是否和那个桌子有同样的问题.
There is one question that I found here Slow scrolling with QTableView on other comp where the user has a similar problem, and he/she found out that the "QTableView is refreshing the items at each scroll / appearance of the window, which is obviously the source of the problem." However I don't know if my table has the same issue as that one.
如何使表的滚动和排序速度更快?问题的根源是什么?
How can I make scrolling and sorting faster for my table? What are the sources of the problem?
推荐答案
问题出在rowCount和数据方法中,因为您没有使用最好的函数.如果使用rowCount,则在使用值时,您将创建一个消耗时间的新数据,在这种情况下,请使用索引.同样,在数据中,您必须使用iloc():
The problem is in the rowCount and data methods since you are not using the best functions. In the case of rowCount when using values you are creating a new data that consumes time, in this case use index. And the same in data, you must use iloc():
def rowCount(self, parent=None):
return len(self._data.index)
# ...
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
return None
这篇关于PyQt5在带有 pandas 的QTableView上极其缓慢地滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!