使用Qt QMainWindow播放图像序列

时间:2014-11-05 20:46:46

标签: image qt sequence playback

我渲染了一个图像序列。我想用简单的QMainWindow或QDialog回报。这就是我所熟悉的。它将图像加载到qlabel中,但我看不到标签正在更新,它只显示最后加载的图像,而且两者之间没有任何内容。 也许有人知道什么?

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)

1 个答案:

答案 0 :(得分:0)

你在循环中改变像素图并睡眠(停止)所有GUI线程,这就是你GUI冻结的原因。

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

这是不正确的。 qLabel.repaint()这是一个糟糕的解决方案,因为它仍会阻止GUI。当然你可以使用processEvents,但这也是不好的方法。

您应该使用QTimer来实现此目的,使用timeout()信号,创建插槽并更改此插槽中的pixmaps。在这种情况下,您的GUI不会被屏蔽,因为QTimer异步工作,图片会成功更改。

与循环和sleep相同的代码只有在此代码将在另一个线程(多线程)中执行时才能帮助您,但由于存在特殊的类QTimer而没有必要。

相关问题