Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an image using QPixmap

Tags:

python

qt

pyside

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

like image 848
Jijoy Avatar asked Dec 16 '22 14:12

Jijoy


1 Answers

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.

like image 161
Liz Avatar answered Dec 26 '22 23:12

Liz