QlistWidget图标-加载速度更快

时间:2019-04-12 00:48:45

标签: python pyside2

我正在用图标填充QlistWidget,并且我注意到在加载窗口时存在滞后。我想知道是否有一种方法可以生成半分辨率图标,或者有其他方法可以加快窗口生成时间?

texture_item = QtWidgets.QListWidgetItem(texture)
texture_pixmap = QtGui.QPixmap(image_path)
texture_icon = QtGui.QIcon()          
self.list_widget_left.setIconSize(QtCore.QSize(105,105))
texture_item.setFont(QtGui.QFont('SansSerif', 10))
texture_icon.addPixmap(texture_pixmap)
texture_item.setIcon(texture_icon)
self.list_widget_left.addItem(texture_item)
texture_item.setTextAlignment(Qt.AlignBottom)

1 个答案:

答案 0 :(得分:2)

可以从多个方面产生延迟:

  • 图像很重,因此解决方案是使用较轻的图标
  • 您有很多图像在同一循环中进行迭代,因此可能的解决方案是提供较小的延迟,以便逐渐加载图像,从而避免显示的视觉延迟。
import os
from PySide2 import QtCore, QtGui, QtWidgets
import shiboken2


def for_loop_files(path, interval=100, extensions=(), parent=None, objectName=""):
    timer = QtCore.QTimer(parent=parent, singleShot=True, interval=interval)
    if objectName:
        timer.setObjectName(objectName)
    loop = QtCore.QEventLoop(timer)
    timer.timeout.connect(loop.quit)
    timer.destroyed.connect(loop.quit)
    for root, dirs, files in os.walk(path):
        for name in files:
            base, ext = os.path.splitext(name)
            if extensions:
                if ext in extensions:
                    if shiboken2.isValid(timer):
                        timer.start()
                        loop.exec_()
                        yield os.path.join(root, name)
            else:
                yield os.path.join(root, name)


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.list_widget = QtWidgets.QListWidget()
        self.list_widget.setViewMode(QtWidgets.QListView.IconMode)
        self.list_widget.setIconSize(QtCore.QSize(128, 128))
        self.list_widget.setResizeMode(QtWidgets.QListView.Adjust)
        self.list_widget.setFlow(QtWidgets.QListView.TopToBottom)
        self.setCentralWidget(self.list_widget)
        self.resize(640, 480)
        QtCore.QTimer.singleShot(0, self.load_icons)

    @QtCore.Slot()
    def load_icons(self):
        for path in for_loop_files(".", extensions=(".png", "jpg"), parent=self, objectName="icon_timer", interval=30):
            it = QtWidgets.QListWidgetItem()
            it.setIcon(QtGui.QIcon(path))
            self.list_widget.addItem(it)

    def closeEvent(self, event):
        timer = self.findChild(QtCore.QTimer, "icon_timer")
        if timer is not None:
            timer.deleteLater()
        super(MainWindow, self).closeEvent(event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
from PySide2 import QtCore, QtGui, QtWidgets
import shiboken2


def for_loop_files(paths, interval=100, parent=None, objectName=""):
    timer = QtCore.QTimer(parent=parent, singleShot=True, interval=interval)
    if objectName:
        timer.setObjectName(objectName)
    loop = QtCore.QEventLoop(timer)
    timer.timeout.connect(loop.quit)
    timer.destroyed.connect(loop.quit)
    for path in paths:
        if shiboken2.isValid(timer):
            timer.start()
            loop.exec_()
            yield path


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.list_widget = QtWidgets.QListWidget()
        self.list_widget.setViewMode(QtWidgets.QListView.IconMode)
        self.list_widget.setIconSize(QtCore.QSize(128, 128))
        self.list_widget.setResizeMode(QtWidgets.QListView.Adjust)
        self.list_widget.setFlow(QtWidgets.QListView.TopToBottom)
        self.setCentralWidget(self.list_widget)
        self.resize(640, 480)
        QtCore.QTimer.singleShot(0, self.load_icons)

    @QtCore.Slot()
    def load_icons(self):
        paths = ["icon1.png", "icon2.png", "icon3.png", "icon4.png"]
        for path in for_loop_files(paths, parent=self, objectName="icon_timer", interval=30):
            it = QtWidgets.QListWidgetItem()
            it.setIcon(QtGui.QIcon(path))
            self.list_widget.addItem(it)

    def closeEvent(self, event):
        timer = self.findChild(QtCore.QTimer, "icon_timer")
        timer.deleteLater()
        super(MainWindow, self).closeEvent(event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())