You are not logged in.

#1 2019-09-08 07:30:52

zee220
Member
Registered: 2017-03-12
Posts: 8

Where to put a programs assets and how to open them.

I recently started learning gui programming and I came across a problem I never had when creating cli programs. Where do I put a programs assets? for example I have a Qt program that attempts to read the icon it's supposed to use at runtime, it does this by opening a file with the path `assets/icon.png`. However this doesn't work unless the user runs the program from the same directory as the executable. For example if my program is in `~/program/`. If I were to run it as such: `~/program/program` it would not be able to find the icon because it doesn't exist in the current directory but rather in `program/assets/icon.png`.

Where are a programs assets normally placed? How do they normally read those assets?

Offline

#2 2019-09-08 11:42:51

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 530
Website

Re: Where to put a programs assets and how to open them.

In /usr/share.

Offline

#3 2019-09-08 12:34:39

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,285
Website

Re: Where to put a programs assets and how to open them.

When installed on a linux system, they would often be placed under /usr/share, but do not assume, and do not hardcode paths as they can/will be different on different systems:
https://doc.qt.io/qt-5/resources.html


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2019-09-08 15:21:37

zee220
Member
Registered: 2017-03-12
Posts: 8

Re: Where to put a programs assets and how to open them.

Thanks trilby and schard for your quick responses they are very helpful. I do have one more question though. I like the idea of storing the need resources into the executable but what are the alternatives to that? I probably won't always be using Qt so is there some other way to do it it or would I just have to hard code paths to /usr/share/?

Offline

#5 2019-09-08 17:39:05

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,285
Website

Re: Where to put a programs assets and how to open them.

Never hardcode paths.  Period.

If you are using a toolkit, it will handle this for you: whether it stores the resources in the binary, or somewhere in the file-hierarchy is up to the toolkit, and it will differ on different systems.  But this way your code is the same regardless.  If you are using a build system, often the build system will define this.

For example, in C (without any toolkit) your makefile or buildsystem should pass PREFIX and other constant definitions that should be used in the code.  In python you'd use something like site.getsitepackages().


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2019-09-08 19:02:22

zee220
Member
Registered: 2017-03-12
Posts: 8

Re: Where to put a programs assets and how to open them.

Thank you that makes sense. I'm currently using GTK where I ran across this issue again with glade files and so far the best info I found is this

https://developer.gnome.org/gtk3/stable … Files.html

I'll look around some more and see what I can do but I'm definitely changing my qt application to use that embedded resource thing. Thank you again.

Offline

#7 2019-09-08 19:46:53

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 22,285
Website

Re: Where to put a programs assets and how to open them.

FWIW, I didn't realize the link I gave was specifically for resources embedded in the binary.  That always struck me as a bit odd.  There likely are other QT mechanisms for getting a resource file path for external resources.  I'm just not familiar with toolkits.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2019-09-08 19:51:42

zee220
Member
Registered: 2017-03-12
Posts: 8

Re: Where to put a programs assets and how to open them.

it wasn't specifically for embedded resources actually. It talked about external binary sources too, I just chose to go with an embedded source because it was easier to setup and it probably wouldn't be worth setting up the external source for a single icon.

Offline

Board footer

Powered by FluxBB