【问题标题】:Python/PySide limit refresh rate of Model/ViewPython/PySide 限制 Model/View 的刷新率
【发布时间】:2015-09-23 05:56:46
【问题描述】:

我有一个这样的模型:

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 个或更多项目时,整个事情变得非常缓慢和滞后。

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

【问题讨论】:

  • 我建议您改为修改 Helper 类,以便它们在本地缓存缩略图。
  • 我也在想同样的事情,但限制刷新将是一个简单的出路。也许只是在项目的初始加载时刷新一次。但我不知道如何控制这种行为。 @three_pineapples
  • 缩略图真的经常变化吗?如果没有,您可以将它们存储在本地,并有一种方法在需要时更新它们。为避免任何延迟,您可以将此更新方法放在单独的线程中。
  • 正如我所说,我可以创建一个系统来对服务器缩略图进行本地缓存,但这需要一些时间来实现。现在,如果我能做到这一点,那将是完美的,这样列表项在初始加载时只请求一次缩略图。 @tmoreau
  • if role == QtCore.Qt.DecorationRole: 多久输入一次?

标签: python performance user-interface pyside


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2014-04-23
    • 2018-08-07
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多