Python/PySide限制模型/视图的刷新率

Python/PySide限制模型/视图的刷新率,python,performance,user-interface,pyside,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

我有一个这样的模型:

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


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

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

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

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

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

我建议你改为修改Helper类,让它们在本地缓存缩略图。我也这么想,但限制刷新是一个简单的方法。也许只需在项目初始加载时刷新一次即可。但我不知道如何控制这种行为@三个菠萝缩略图真的经常变化吗?如果没有,您可以将它们存储在本地,并在需要时使用方法进行更新。为了避免任何延迟,您可以将此更新方法放在单独的线程中。现在,如果我能让列表项在初始加载时只请求一次缩略图,那就太完美了@tmoreau如果role==QtCore.Qt.DecorationRole:输入
的频率是多少?我建议您改为修改Helper类,以便它们在本地缓存缩略图。我也在想同样的事情,但限制刷新将是一个简单的方法。也许只需在项目初始加载时刷新一次即可。但我不知道如何控制这种行为@三个菠萝缩略图真的经常变化吗?如果没有,您可以将它们存储在本地,并在需要时使用方法进行更新。为了避免任何延迟,您可以将此更新方法放在单独的线程中。现在,如果我能让列表项在初始加载时只请求一次缩略图,那就太完美了@tMoreAuRole==QtCore.Qt.DecorationRole:
输入的频率是多少?