gpt4 book ai didi

python - QlistWidget 图标 - 加载速度更快

转载 作者:行者123 更新时间:2023-12-01 01:02:41 31 4
gpt4 key购买 nike

我正在用图标填充 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)

最佳答案

有几个方面会产生延迟:

  • 图像很重,解决方案是使用较轻的图标
  • 您有许多图像在同一个循环中迭代,那么一个可能的解决方案是提供一个小的延迟,以便逐渐加载图像,避免您指示的视觉延迟。
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_())

关于python - QlistWidget 图标 - 加载速度更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55643005/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com