我有下面的代码,我注意到当我按ctrl + A来选择所有行时,感觉有些迟钝,大约需要1秒钟才能完成选择。但是,我还注意到,如果用self.horizontalHeader().hide()
隐藏水平标题,则滞后完全消失了!为什么会这样?而如何摆脱滞后但保留标题呢?
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtCore import Qt
import pandas as pd
import numpy as np
import sys
class SeriesModel(QtCore.QAbstractTableModel):
def __init__(self, series):
super().__init__()
self.series = series
def columnCount(self, parent=None):
return 1
def rowCount(self, parent=None):
return len(self.series)
def data(self, index, role=None):
row = index.row()
col = index.column()
if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.ToolTipRole:
return str(self.series[row])
# The headers of this table will show the level names of the MultiIndex
def headerData(self, section, orientation, role=None):
if role in [QtCore.Qt.DisplayRole, QtCore.Qt.ToolTipRole]:
if orientation == Qt.Horizontal:
return str(self.series.name)
class SeriesView(QtWidgets.QTableView):
def __init__(self, series):
super().__init__()
self.setModel(SeriesModel(series))
# self.horizontalHeader().hide()
# Examples
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
series = pd.Series(np.random.randn(1000000)).rename('MyData')
view = SeriesView(series)
view.show()
sys.exit(app.exec_())
答案 0 :(得分:2)
问题在于QTableView标头中默认启用了highlightSections属性。此属性意味着,如果选择了某个项目,则标头中暗示重绘的项目也会被选中,在您的情况下,您有1000000,因此它将被重绘类似的次数。因此,可能的解决方案是禁用该属性:
class SeriesView(QtWidgets.QTableView):
def __init__(self, series):
super().__init__()
self.setModel(SeriesModel(series))
self.horizontalHeader().setHighlightSections(False)