How to specify image path in Qt project?
-
Hello! I'm currently working on a Qt project, and I would like to specify the path to the images I have included in my project directory. Images are located in a folder called "images" at the root of the project. My intention is to make the path flexible so that the project can be shared and run from different computers without having to manually adjust the paths.
Currently I'm using something like this in my code and it doesn't work:
Cancelbutton->setIcon(QIcon("images/cancel.png"));
-
Hi and welcome to devnet,
One possible simple way to achieve what you want is to use Qt's resources system. This allows to embed files within the executable.
-
Hello! I'm currently working on a Qt project, and I would like to specify the path to the images I have included in my project directory. Images are located in a folder called "images" at the root of the project. My intention is to make the path flexible so that the project can be shared and run from different computers without having to manually adjust the paths.
Currently I'm using something like this in my code and it doesn't work:
Cancelbutton->setIcon(QIcon("images/cancel.png"));
@Jsla
The problem is that the "project directory" which exists while you develop simply does not "exist" at runtime when you deploy elsewhere/to other people. By which I mean there is no way to "locate" it.You then have 3 choices:
-
Use Qt's QStandardPaths Class to generate a full path to one of the standard directories and use that. The problem is that it's not easy to deploy your file(s) there in the first place,.
-
Use Qt's QCoreApplication::applicationDirPath() to locate where the executable lives. Use that to generate a full path to some
images
sub-directory from there where the images have been provided. -
As @SGaist says use Qt's resource system to embed the images into the actual executable and access them there. This is perhaps the easiest, requires you to specify you are using resources at development time.
-