Changing image to widget

Hi

I’m looking for a way to load png images onto an gtk::Image for my card game. But I have two problems

  • I can’t find a way to change the image from the image widget
  • I don’t understand how to load the image from a path

I looked around but I can’t find any forum that gave me an answer

Do you need something like Gtk.Image.new_from_file?

I have the image in the Struct of window.rs, I was looking for a way to change the path of the image without creating a new widget from scratch. But if new_from_file is the only way it’s ok

(Edit: I found that gtk::Image::set_from_file() works, unfortunately tha path works only if I pass the absolute one. Is there a way to pass as a parameter the relative path of the image, like: “data/images/5-Spade.png” )

Is there a way to pass as a parameter the relative path of the image

Nope, but as long as the image is stored in a standard location, you can probably get the absolute path pretty easily.

Yes, the image is always stored into the data directory, my question was how to pass the path parameter without hardcoding it

Hi,

If by “data directory” you mean “/usr/share” or similar (the exact path may depend on the distro, in flatpak it will be “/app/share”), then you can get it programmatically with GLib.get_system_data_dirs

the data directory I meant was the directory inside the project structure that gnome-builder creates for every new project. so <Project_Path>/data/images/image.png. And I need to ask if I can use paths or I need to link the image into the binary during the build or during the export phase

If you need a reliable way to find the images, you can either:

  • link them directly into the binary using gresources, and set the image with Gtk.Image.set_from_resource (“resource://virtual/path/to/image.png”)
  • configure meson to install the images under get_option('prefix') / get_option('datadir') / meson.project_name() / image.png, then use GLib.get_system_data_dirs to lookup for them

The 1st option is more reliable but will increase the binary size. Typically used for small/mid-sized images that always/often need to be loaded.

The 2nd option is better suited for bigger images that only need to be loaded on demand.

Since It’s a card game I see the first option as more appropriate, many thanks for the answer

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.