Python / PySide限制模型/视图

时间:2015-07-06 07:21:14

标签: python performance user-interface pyside

我有一个这样的模型:

class GeneralAssetIconModel(QtCore.QAbstractListModel):
    def __init__(self, parent=None):
        super(GeneralAssetIconModel, self).__init__(parent)
        self._data = []

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

    def data(self, index, role):
        if role == QtCore.Qt.DecorationRole:
            taskModel = self._data[index.row()]
            ext = taskModel.getData().obj['type']['ext']

            pix = QtGui.QPixmap(160, 160)
            pix.load('Assets/thumbnail-default.jpg')

            if ext == '.ma':
                pass
            if ext == '.psd':
                pix = PhotoshopHelper.getLatestThumbnail(taskModel)
            if ext == '.ai':
                pix = IllustratorHelper.getLatestThumbnail(taskModel)
            if ext == '.mra':
                pix = MariHelper.getLatestThumbnail(taskModel)
            if ext == '.indd':
                pix = IndesignHelper.getLatestThumbnail(taskModel)

我遇到的问题是" getLatestThumbnail"函数始终从服务器文件中读取缩略图数据,并尝试在视图中显示它,此操作非常慢。当我在列表中显示30个或更多项目时,整个事情变得非常缓慢和滞后。

有没有办法限制视图从模型请求数据的次数?

1 个答案:

答案 0 :(得分:0)

我通过缓存模型本身的所有数据成功优化了缩略图加载。也许这不是最好的方法,但现在它的工作速度非常快。这是模型现在的样子。

class GeneralAssetIconModel(QtCore.QAbstractListModel):
    def __init__(self, parent=None):
        super(GeneralAssetIconModel, self).__init__(parent)
        self._data = []
        self.cache = {}

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

    def data(self, index, role):
        index_row = index.row()
        if index_row in self.cache and 'DecorationRole' in self.cache[index_row] and 'DisplayRole' in self.cache[index_row]:
            if role == QtCore.Qt.DecorationRole:
                return self.cache[index_row]['DecorationRole']
            if role == QtCore.Qt.DisplayRole:
                return self.cache[index_row]['DisplayRole']
        else:
            if index_row not in self.cache:
                self.cache[index_row] = {}
            if role == QtCore.Qt.DecorationRole:
                taskModel = self._data[index_row]
                ext = taskModel.getData().obj['type']['ext']

                pix = QtGui.QPixmap(160, 160)
                pix.load('Assets/thumbnail-default.jpg')

                if ext == '.psd':
                    pix = PhotoshopHelper.getLatestThumbnail(taskModel)
                if ext == '.ai':
                    pix = IllustratorHelper.getLatestThumbnail(taskModel)
                if ext == '.mra':
                    pix = MariHelper.getLatestThumbnail(taskModel)
                if ext == '.indd':
                    pix = IndesignHelper.getLatestThumbnail(taskModel)
                if ext == '.ma':
                    pass

                self.cache[index_row]['DecorationRole'] = QtGui.QIcon(pix)
                return QtGui.QIcon(pix)
            if role == QtCore.Qt.DisplayRole:
                self.cache[index_row]['DisplayRole'] = self._data[index_row].getName()
                return self._data[index_row].getName()