如果 QLabel() 是 JPG 图像,则不会加载像素图 [英] QLabel() won't load a pixmap if it is a JPG image

查看:41
本文介绍了如果 QLabel() 是 JPG 图像,则不会加载像素图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 QLabel() 显示来自文件的 pixmap JPG 图像(它不能在资源文件中,因为它是从web),但我在加载它时遇到问题.代码相当简单:

I am trying to have a QLabel() display a pixmap JPG image from a file (which can't be in a resource file since it is downloaded from the web), but I am having problems loading it. The code is fairly simple:

label = QLabel()
label.setPixmap(QPixmap("image.jpg"))

它适用于 PNG 文件,但不适用于 JPG 文件.我在谷歌上搜索了很多,发现我需要将 "imageformats" 文件夹放在我的脚本所在的同一文件夹中.然而,即使这样做(是的,qjpeg4.dll 和其他人在那里),它仍然不起作用.我也试过

It works with PNG files, but it doesn't work with JPG files. I have Googled quite a bit and found that I needed to place the "imageformats" folder in the same folder where my script is located. However, even after doing this (and yes, qjpeg4.dll and others are there), it still doesn't work. I've also tried doing

path = r"C:\Users\Deusdies\Documents\Work\myapp\imageformats"
app.addLibraryPath(path)

但这也无济于事.

同样,它加载 PNGs 就好了,但它不会加载 JPGs.我之前也注意到它也不会加载 ICO,但我认为这是一个无关的问题 - 但现在似乎不是这样.

Again, it loads PNGs just fine, but it won't load JPGs. I've also noticed even before that it won't load ICO either, but I thought of it as an unrelated issue - however it doesn't seem that way now.

值得注意的是,此时应用程序并未转换为 .exe - 它是通过 PowerShell 通过 python.exe 解释器运行的代码>.

It is worth noting that the application is not converted to an .exe at this point - it is ran through python.exe interpreter via PowerShell.

我的开发环境是 Windows 7 x64,PySide 1.1.0

My development environment is Windows 7 x64, PySide 1.1.0

我该如何解决这个问题?

How can I solve this problem?

推荐答案

我解决了这个问题.首先,路径应如下所示:

I solved the problem. First, path should look like this:

path = r"C:\Users\Deusdies\Documents\Work\myapp"

(所以没有图像格式"部分)

(so without the "imageformats" part)

其次,我是个白痴.在执行 addLibraryPath()

And second, I was an idiot. I created an instance of the QDialog() class before doing the addLibraryPath()

这篇关于如果 QLabel() 是 JPG 图像,则不会加载像素图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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