简体   繁体   中英

Loading an image using QPixmap

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

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:

pixmap = QPixMap(r'C:\filename.jpeg')

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.

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 is qjpeg4.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 and QtGui4.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 the UI .

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:

[Paths]
Prefix = C:/Python27/lib/site-packages/PySide
Binaries = .
Plugins = plugins
Translation = translation

C:/Python27/lib/site-packages/PySide is part of my system PATH variable and ahead of any PyQt (or other Qt) directories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM