PyQt4:QTableView非常慢

时间:2017-04-13 12:45:57

标签: python performance pyqt4 qtableview

您好我使用PyQt4构建了一个GUI,它有一个QTableView,但它很慢。 每当我点击其中一个单元格时,它会打开一个弹出窗口(非常慢)。 但滚动时它也很安静。 我继承了QtCore.QAbstractTableModel:

class RegressionTableModel(QtCore.QAbstractTableModel):

ROW_BATCH_COUNT = 15

def __init__(self, data=None, header=None, parent=None):
    super(RegressionTableModel, self).__init__(parent)
    if data is None:
        self._data = []
    else:
        self._data = data
    self.tooltips = {}
    if header is None:
        self._header = []
    else:
        self._header = header
    self.rows_loaded = RegressionTableModel.ROW_BATCH_COUNT

def rowCount(self, parent=None):
    return len(self._data)

def columnCount(self, parent=None):
    return len(self._data[0]) if self.rowCount() else 0

def data(self, index, role=QtCore.Qt.DisplayRole):
    if role == QtCore.Qt.TextAlignmentRole:
        return QtCore.Qt.AlignCenter
    if role == QtCore.Qt.BackgroundRole:
        if 'Pass' in self._data[index.row()][index.column()]:
            return QtGui.QColor(193, 204, 137)
        elif 'Fail' in self._data[index.row()][index.column()]:
            return QtGui.QColor(176, 102, 96)
        elif any(x in self._data[index.row()][index.column()] for x in ['pre-test', 'post-test']):
            return QtGui.QColor(106, 125, 142)
    if role == QtCore.Qt.ForegroundRole:
        if 'Pass' in self._data[index.row()][index.column()]:
            return QtGui.QBrush(QtCore.Qt.black)
        elif 'Fail' in self._data[index.row()][index.column()]:
            return QtGui.QBrush(QtCore.Qt.white)
        elif any(x in self._data[index.row()][index.column()] for x in ['pre-test', 'post-test']):
            return QtGui.QBrush(QtCore.Qt.white)
    if role == QtCore.Qt.FontRole:
        if any(x in self._data[index.row()][index.column()] for x in ['Pass', 'Fail', 'pre-test', 'post-test']):
            font = QtGui.QFont()
            font.setBold(True)
            return font
    if role == QtCore.Qt.DisplayRole:
        if 0 <= index.row() < self.rowCount():
            if 0 <= index.column() < self.columnCount():
                return self._data[index.row()][index.column()]
    if role == QtCore.Qt.ToolTipRole:
        try:
            operation_params = ""
            for line in self.tooltips[index.row()][index.column()]:
                operation_params += line + "\n"
            return operation_params
        except KeyError:
            return "Nothing to see"

def headerData(self, index, orientation, role):
    if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
        return QtCore.QVariant("Client %s" % index)
    elif orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
        return QtCore.QVariant(index)
    return QtCore.QVariant()

我听说它可能工作缓慢,也可以通过自定义模型解决。但它不适合我。

0 个答案:

没有答案