在 QMovie 中加载动画 gif 数据 [英] Loading animated gif data in QMovie

查看:88
本文介绍了在 QMovie 中加载动画 gif 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Qt(特别是 PySide)非常陌生,我正在尝试编写一个脚本,将文件中的动画 gif 加载到 QByteArray,然后加载到 QMovie.从文件转到 QByteArray 的原因是因为我无法将该 gif 文件保存在内存中.我希望能够以这种方式存储动画 gif,以便稍后将其写出到 JSON 文件(因此是 QByteArray).我尝试使用 here 和虽然没有出现错误,但动画 gif 也没有出现.(那里可能有东西,但我什么也没看到.)简而言之,我的代码如下所示:

I'm very new to Qt (specifically PySide) and I'm trying write a script that loads an animated gif from file into a QByteArray and then into a QMovie. The reason for going from file to the QByteArray is because I cannot keep that gif file in memory. I want to be able to store the animated gif in such a way that it can be written out to a JSON file later (hence the QByteArray). I've tried using ekhumoro's answer from here and although no errors showed up, the animated gif also doesn't show up. (There could be something there but I don't see anything.) My code, in a nutshell, looks like this:

data = open("img.gif", "rb").read()
self.bArray = QtCore.QByteArray(data)
self.bBuffer = QtCore.QBuffer(self.bArray)
self.bBuffer.open(QtCore.QIODevice.ReadOnly)
self.movie = QtGui.QMovie(self.bBuffer, 'GIF')
self.movieLabel.setMovie(self.movie) # a QLabel
self.movie.start()

我想稍后将 self.bArray 的内容存储到 JSON 文件中.

I want to store the contents of self.bArray to a JSON file later.

当我为 QMovie 构造函数提供文件路径时,我可以看到动画 gif,但随后我将无法将 gif 的内容保存到 JSON 文件中.

I can see the animated gif when I give the QMovie constructor the file path but then I won't be able to save the contents of the gif to a JSON file.

我想知道数据是否没有被正确读取或没有正确传递给 QMovie.

I'm wondering if the data is not being read in properly or not being passed to QMovie properly.

有什么想法吗?

谢谢!

推荐答案

这看起来像是 PySide 的错误,因为相同的代码在 PyQt 中运行良好.

This looks like a PySide bug, as the same code works perfectly fine in PyQt.

该错误似乎在 QMovie 构造函数中,它不会从传递给它的设备中读取任何内容.解决方法是显式设置设备,如下所示:

The bug seems to be in the QMovie constructor, which does not read anything from the device passed to it. A work-around is to set the device explicitly, like this:

import sys
from PySide import QtCore, QtGui
# from PyQt4 import QtCore, QtGui

app = QtGui.QApplication(sys.argv)

data = open('anim.gif', 'rb').read()
a = QtCore.QByteArray(data)
b = QtCore.QBuffer(a)

print('open: %s' % b.open(QtCore.QIODevice.ReadOnly))

m = QtGui.QMovie()
m.setFormat('GIF')
m.setDevice(b)

print('valid: %s' % m.isValid())

w = QtGui.QLabel()
w.setMovie(m)
m.start()

w.resize(500, 500)
w.show()
app.exec_()

print('pos: %s' % b.pos())

这篇关于在 QMovie 中加载动画 gif 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆