QMovie意外参数QBuffer

时间:2018-08-14 02:15:54

标签: python python-3.x pyqt5

嗨,我认为QMovie可以使用QBuffer吗? 这是我的代码。

a = QByteArray(img)
b = QBuffer(a)
self.movie = QMovie(b, 'GIF')

1 个答案:

答案 0 :(得分:0)

您要使用第二个构造函数:

  

QMovie::QMovie(QIODevice *device, const QByteArray &format = QByteArray(), QObject *parent = nullptr)

并且如您所见,第二个参数是一个QByteArray,可以用字节替换,因此在下一部分中,我向您展示一个示例:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    # load data from
    path = "congratulations.gif"
    file = QtCore.QFile(path)
    if not file.open(QtCore.QIODevice.ReadOnly):
        sys.exit(-1)
    ba = file.readAll()


    buf = QtCore.QBuffer(ba)
    if not buf.open(QtCore.QIODevice.ReadOnly):
        sys.exit(-1)

    movie = QtGui.QMovie(buf, b"gif")
    w = QtWidgets.QLabel()
    w.setMovie(movie)
    movie.start()
    w.show()
    sys.exit(app.exec_())