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

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

最佳答案

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


  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_())

10-07 13:13