I'm writing an app where a QListWidget
is used to show gifs previews using QMovie
. Everything works fine but there's an increasing slow down while I push the button and repopulate the ListWidget. I was thinking that calling QListWidget.clear()
will delete the objects from memory but I'm not sure is actually happening. Maybe there's a more efficient way of doing this but I couldn't find anything elsewhere. Here's some code (I cannot give gifs files but any gif should work):
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
import os, glob
class gifPreviewWidget(QWidget):
def __init__(self, gifPath, parent=None):
super(gifPreviewWidget, self).__init__(parent)
label = QLabel()
label.setScaledContents(True)
movie = QMovie(gifPath)
movie.setSpeed(100)
label.setMovie(movie)
movie.start()
nameLabel = QLabel(os.path.split(gifPath)[1].split('.')[0])
layout = QVBoxLayout()
layout.addWidget(label)
print(gifPath)
self.setLayout(layout)
class gifDelegate(QItemDelegate):
def __init__(self, parent=None, *args):
QItemDelegate.__init__(self, parent, *args)
def paint(self, painter, option, index):
painter.save()
# set background color
painter.setPen(QPen(Qt.NoPen))
# set selection color
if option.state & QStyle.State_Selected:
painter.setBrush(QBrush(Qt.blue))
else:
painter.setBrush(QBrush(Qt.NoBrush))
painter.drawRect(option.rect)
painter.restore()
class MainUiClass(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MainUiClass, self).__init__(parent)
self.layout=QVBoxLayout()
self.gifDelegate = gifDelegate()
self.previewListWidget = QListWidget()
self.previewListWidget.setViewMode(QtWidgets.QListView.IconMode)
self.previewListWidget.setResizeMode(QListWidget.Adjust)
self.previewListWidget.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.previewListWidget.setMovement(QListView.Static)
self.previewListWidget.setSpacing(1)
self.previewListWidget.setItemDelegate(self.gifDelegate)
self.pushButton = QPushButton('change gif dir')
self.layout.addWidget(self.previewListWidget)
self.layout.addWidget(self.pushButton)
self.setLayout(self.layout)
self.pushButton.clicked.connect(self.onCategoryClick)
def onCategoryClick(self):
filePath = '/PATH/TO/LOTS/OF/GIFS'
print(filePath+'/*.gif')
glob.glob(filePath+'/*.gif')
if glob.glob(filePath+'/*.gif') != []:
print('ok')
gifList = glob.glob(filePath+'/*.gif')
self.previewListWidget.clear()
for gif in gifList:
item=QListWidgetItem(self.previewListWidget)
itemWidget = gifPreviewWidget(gif)
item.setSizeHint(itemWidget.sizeHint())
self.previewListWidget.addItem(item)
self.previewListWidget.setItemWidget(item,itemWidget)
if __name__ == '__main__':
w = MainUiClass()
w.show()
sys.exit(app.exec_())
By pressing the button often, the app starts to slow down a lot.
Usually, you don't have to stop QMovie
explicitly when you're about to delete it anyway. But here, it does not get deleted because it is not stopped (it's still playing).
I guess that kind of unexpected behavior is what you pay for when you use a language with a garbage collector.
Solution: stop the movie when the item is deleted. Add
class gifPreviewWidget(QWidget):
def __del__(self):
self.movie.stop()