使用 QPixmap 加载图像
我有一个图像文件,如 C:/44637landscapes-2007.jpg
我想使用 PySide 使用 QPixmap
加载此文件,
我尝试如下。
pixmap = QPixmap('C:/44637landscapes-2007.jpg')
但文档说像 QPixmap(':/xxxxxx.jpeg')
。 ':'
是什么意思?
如何在 'C:\'
加载图像?
编辑:问题在于尝试加载 "JPEG"
。它能够加载 "PNG"
,没有任何问题。那么,我还需要做什么来加载 "JPEG"
?
谢谢 吉乔伊
I have an image file , as C:/44637landscapes-2007.jpg
I want to load this file using QPixmap
using PySide
I tried as following .
pixmap = QPixmap('C:/44637landscapes-2007.jpg')
But documentation says like QPixmap(':/xxxxxx.jpeg')
. What does ':'
means ?
How do I load an image at 'C:\'
?
EDIT : The problem was with trying to load "JPEG"
. It was able to load "PNG"
, with no issues. So what else , I need to do to load "JPEG"
?
Thanks
Jijoy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Qt 对图像做了一些奇怪的事情。如果你只使用 png,你永远不会有问题。其他图像格式的处理方式略有不同。
如果您查看 Qt 目录(所有二进制文件和源代码都在其中),您会发现一个名为:
plugins\imageformats
的目录在该目录中有大量
dll
(或.so
)对应 Qt 支持的每种图像格式。就您的情况而言,我认为您感兴趣的是 qjpeg4.dll现在,当您运行应用程序并尝试使用 jpeg 时,Qt 将尝试加载此插件,但如果找不到它,那么您的图像将不会显示。
我们对 ico 文件也有类似的问题。它在我们的开发环境中工作,因为 Qt 文件夹位于我们的
PATH
中,因此它神奇地找到了这个插件。然而,当我们发布我们的软件时,我们只会分发我们需要的那些 dll,因此它不起作用。经过大量调查后,这就是我们所做的:
我们的应用程序位于 bin/ 目录中。这是放置 exe 和所有 Qt dll 的位置。
QtCore4.dll
和QtGui4.dll
等...在 bin 目录下我们创建一个名为
'imageformats' 的子目录
放入
qjpeg4.dll
以及您可能需要的任何其他图像插件。修改代码如下:
QApplication _App(argc, argv);
// 查找 bin 目录的路径 - 无论您的 exe 所在的位置。
QString _Path = _PathToBin + QString(QDir::separator()) + "imageformats");
// 这一行提供了 Qt 的路径,告诉它在此处查找插件
QApplication::addLibraryPath(_Path);
//初始化主窗体
返回_App.exec();
这应该确保 Qt 能够识别
jpeg
处理插件,并在UI
中加载您的图像。Qt does something a little weird with images. If you are using only pngs you will never have a problem. The handling of other image formats is handled a little differently.
If you take a look in your Qt directory (where all the binaries and source is) you will find a directory called:
plugins\imageformats
In that directory there are a ton of
dlls
(or.so
) one for each image format that Qt supports. In your case I presume the one that interests you isqjpeg4.dll
Now, when you run your application and you try to use a
jpeg
, Qt will try to load this plugin, but if it can't find it, then your image won't show up.We had a similar problem with ico files. It worked in our development environment because the Qt folder was in our
PATH,
and therefore it magically found this plugin. However, when we were releasing our software we would distribute only those dlls that we needed, and therefore it didn't work.After much investigation this is what we did:
Our application is located in a bin/ directory. This is where the exe, and all Qt dlls are put.
QtCore4.dll
andQtGui4.dll
etc...Under the bin directory we create a subdirectory called
'imageformats'
Put
qjpeg4.dll
and any other image plugins you might need.Change the code as follows:
QApplication _App(argc, argv);
// Find the path of your bin directory - whereever your exe resides.
QString _Path = _PathToBin + QString(QDir::separator()) + "imageformats");
// This line is providing a path to Qt telling it to look here for plugins
QApplication::addLibraryPath(_Path);
// Initialize the main form
return _App.exec();
This should make sure that Qt is aware of the
jpeg
handling plugin, and load your image in theUI
.您可以通过提供文件的路径名来构造 QPixMap 的实例。当您尝试代码时出现什么错误?将您的代码更改为以下内容:
您所指的文档是加载 Qt 资源的方式。我们可以将应用程序所需的图像转换为 Qt 资源,以便以与平台无关的方式加载它。有关执行此操作的步骤,请参阅Qt 资源系统
文档。
You can construct an instance of QPixMap by providing the path name of the file. What is the error you get when you try your code ? Change your code to something like:
The documentation what you are referring to is the way you would load a Qt resource. One can convert an image that is needed by the application to a Qt resource so as to load it in a platform independent manner. For steps to do so refer to The Qt Resource System
documentation.
如果您尝试将 Python 代码构建为可执行文件,这涵盖了 .ico 文件的问题,但该过程对于 JPEG 是相同的:
PyQt/PySide - 图标显示
如果运行时遇到问题从您安装的解释器中,您的路径变量可能未正确设置,您的 qt.conf (在基本 python 目录中)已被删除/更改,或者您没有 qjpeg4.dll 应该在的位置(
< ;python 目录>\Lib\site-packages\PySide\plugins\imageformats
)。我的 qt.conf 看起来像:
C:/Python27/lib/site-packages/PySide 是我的系统 PATH 变量的一部分,并且位于任何 PyQt(或其他 Qt)的前面目录。
If you're trying to build your Python code to an executable this covers the issue for .ico files but the process is the same for JPEG:
PyQt/PySide - icon display
If you're having problems when running from your installed interpreter then your path variable probably isn't set correctly, your qt.conf (in the base python dir) has been deleted/changed or you don't have qjpeg4.dll where its supposed to be (
<python dir>\Lib\site-packages\PySide\plugins\imageformats
).My qt.conf looks like:
C:/Python27/lib/site-packages/PySide
is part of my system PATH variable and ahead of any PyQt (or other Qt) directories.你可以使用这个:
You can use this: