Python QTableView horizontalHeader使其非常滞后?
我有下面的代码,我注意到当我按ctrl+A选择所有行时,感觉很滞后,大约需要1秒来完成选择。但是,我也注意到,如果我用Python QTableView horizontalHeader使其非常滞后?,python,pyqt,pyqt5,qtableview,Python,Pyqt,Pyqt5,Qtableview,我有下面的代码,我注意到当我按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
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_())
问题在于默认情况下,在QTableView标头中启用了highlightSections属性。此属性意味着,如果选择了一个项目,则标题中暗示重新绘制的项目也将被选择,在您的情况下,您有1000000个项目,因此它将被重新绘制类似的次数。因此,一个可能的解决方案是禁用该属性:
class系列视图(qtwidts.QTableView):
def ___;初始(自,系列):
super()。\uuuu init\uuuuu()
self.setModel(系列模型)
self.horizontalHeader().setHighlightSections(False)