PyQt - 从 URL 设置 QLabel 图像 [英] PyQt - Set QLabel image from URL

查看:116
本文介绍了PyQt - 从 URL 设置 QLabel 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将图像从 URL 设置为 QLabel.但我没有运气.这是我现在尝试这样做的方法:

I've been trying to set an image to a QLabel from a URL. But im having no luck. Here is how im trying to do it right now:

import urllib, cStringIO
img_file = cStringIO.StringIO(urllib.urlopen(image_url).read())
image_file = Image.open(img_file)

然后将其设置为 QImage:

Then setting this to a QImage:

final_image = QImage(image_file)

self.emit(SIGNAL("finished(QImage)"),
    final_image
)

图像正在从线程传递回主 GUI 中的方法.

The image is getting passed back from a thread to a method in the main GUI.

def set_image(self, final_image):
    self.main_picture_pixmap = QPixmap.fromImage(final_image).scaled(
        QSize(self.picture_label.size()), 
        Qt.KeepAspectRatio, 
        Qt.FastTransformation
    )
    self.picture_label.setPixmap(self.main_picture_pixmap)

这样做我得到错误:

QPixmap::scaled: Pixmap is a null pixmap

有没有办法解决这个问题,或者有不同的方法来做到这一点?

Is there anyway to fix this, or a different way to do this?

推荐答案

QImage.load 需要一个文件名,而不是图像数据.如果你想这样做,你需要使用 QImage.loadFromData.

这篇关于PyQt - 从 URL 设置 QLabel 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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