Python/PySide限制Model/Vi的刷新率

2024-05-23 14:30:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个模型:

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个或更多的项目显示在列表中,整个事情变得非常缓慢和滞后。你知道吗

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


Tags: 模型selfdataindexifinitdefext
1条回答
网友
1楼 · 发布于 2024-05-23 14:30:32

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

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()

相关问题 更多 >