Search code examples
imageqtsequenceplayback

Play image sequence using Qt QMainWindow


I have an image sequence rendered out. which I want to payback in a simple QMainWindow or QDialog. This is what I have sofar. It loads the images into the qlabel, but I cant see the label being updated, its just show the last loaded image, and nothing in between. Maybe someone knows something?

from PySide import QtCore, QtGui
import shiboken
import maya.OpenMayaUI as apiUI
import time

def getMayaWindow():
    """
    Get the main Maya window as a QtGui.QMainWindow instance
    @return: QtGui.QMainWindow instance of the top level Maya windows
    """
    ptr = apiUI.MQtUtil.mainWindow()
    if ptr is not None:
        return shiboken.wrapInstance(long(ptr), QtGui.QWidget)


class Viewer(QtGui.QMainWindow):

def __init__(self, parent = getMayaWindow()):
    super(Viewer, self).__init__(parent)
    self.setGeometry(400, 600, 400, 300)  
    self.setUi()   

def setUi(self):
    self.label = QtGui.QLabel()
    self.setCentralWidget(self.label)

def showUi(self):
    self.show()

def loadImage(self, path):
    self.label.clear()
    image = QtGui.QImage(path)
    pp = QtGui.QPixmap.fromImage(image)
    self.label.setPixmap(pp.scaled(
            self.label.size(),
            QtCore.Qt.KeepAspectRatio,
            QtCore.Qt.SmoothTransformation))

x = Viewer()
x.showUi()
for i in range(1, 11):    
    x.loadImage("C://anim%03d.png" % i)
    time.sleep(0.5)

Solution

  • You change pixmaps in loop and sleep (stop) all GUI thread, that's why your GUI freeze.

    http://www.tutorialspoint.com/python/time_sleep.htm

    It is not correct. qLabel.repaint() it is bad solution because it still blocks GUI. Of course you can use processEvents but it is bad approach too.

    You should use QTimer for this purpose, use timeout() signal, create slot and change pixmaps in this slot. In this case your GUI will not be blocked because QTimer works asynchronously and images will be successfuly changed.

    Same code with loop and sleep can help you only when this code will execute in another thread (multi threading) but it is not necessary because there is special class QTimer.