c++qtqt-creatorqpixmap

How to use QPixmap so that only the project folder has to be used in the address


I am using QPixmap in order to output pictures to my GUI and I have it working so far as such:

QPixmap background("C:/Users/sam/OOC++/CarProject/Images/Background.png");

But I realised that in doing this that if I wanted to use this code in another place, such as someone else's computer that this wouldn't work as they will not necessarily have the picture in the same place.

So I just wanted to know if there was a better, more general way of doing this. I was hoping that there was a shortcut so that it would go directly to the project that is being used and look there automatically so that I could just have something like:

QPixmap background("Images/Background.png");

Cheers!


Solution

  • You can specify the path relative to the executable and then use QCoreApplication::applicationDirPath() to build the right path. You can use install command to copy the images automaticaly after build.

    Or better solution would be to put the image in the resource file. Then you specify the path in the code like for ex. ":/Images/Background.png"