Right source page for latest stable gtkmm4 - right function: image::create_from_resource?

is this page Index of /sources/gtkmm/4.18/
right for latest stable gtkmm4 ?
is header file image.h from this file right?
which function for load image from resource is right ?
Gtk::Image::create_from_resource
or
Gtk::Image().set_from_resource
?

Hi @Peta-T,

Yes, Index of /sources/gtkmm/4.18/ is the latest stable release.

As you can see, there’s also version 4.19 available for download, however GTK follows an even-odd versioning scheme: even releases are stable, odd releases are unstable (canary releases, not meant for general use)

Go with Gtk::Image::create_from_resource

There are two samples of code, first with this: Gtk::Image().set_from_resource
and second not working with this: Gtk::Image::create_from_resource

// External function to get the GResource for the application.
extern GResource *resources_get_resource(void);

// MyWindow class definition, inheriting from Gtk::Window.
class MyWindow : public Gtk::Window
{
public:
// Constructor.
MyWindow();
// Destructor, overriding the base class destructor.
~MyWindow() override;

protected:
// Signal handlers for button clicks.
void on_button_new_clicked();
void on_button_open_clicked();
void on_button_save_clicked();
void on_button_svg_clicked();

// Member widgets for the window layout and controls.
Gtk::Box m_VBox;           // Vertical box to arrange widgets.
Gtk::Box m_ToolbarBox;     // Horizontal box for toolbar buttons.
Gtk::Button m_ButtonNew;    // Button for "New File".
Gtk::Button m_ButtonOpen;   // Button for "Open File".
Gtk::Button m_ButtonSave;   // Button for "Save File".
Gtk::Button m_ButtonSVG;    // Button for a custom SVG icon.
Gtk::Label m_Label;        // Label to display messages.

};

// MyWindow constructor implementation.
MyWindow::MyWindow()
: m_VBox(Gtk::Orientation::VERTICAL), // Initialize vertical box.
m_ToolbarBox(Gtk::Orientation::HORIZONTAL), // Initialize horizontal toolbar box.
m_Label(“Click a button on the toolbar.”) // Initial text for the label.
{
// Set the window title.
set_title(“Toolbar with Icons Example (Fixed for gtkmm4)”);
// Set the default size of the window.
set_default_size(400, 200);
// Set the main vertical box as the child of the window.
set_child(m_VBox);

// Configure the "New" button.
m_ButtonNew.set_icon_name("document-new"); // Use a standard icon.
m_ButtonNew.set_tooltip_text("New File");  // Set tooltip text.
m_ButtonNew.set_has_frame(false);          // Remove button frame for a toolbar look.
m_ToolbarBox.append(m_ButtonNew);          // Add button to the toolbar box.

// Configure the "Open" button.
m_ButtonOpen.set_icon_name("document-open"); // Use a standard icon.
m_ButtonOpen.set_tooltip_text("Open File");  // Set tooltip text.
m_ButtonOpen.set_has_frame(false);           // Remove button frame.
m_ToolbarBox.append(m_ButtonOpen);           // Add button to the toolbar box.

// Configure the "Save" button.
m_ButtonSave.set_icon_name("document-save"); // Use a standard icon.
m_ButtonSave.set_tooltip_text("Save File");  // Set tooltip text.
m_ButtonSave.set_has_frame(false);           // Remove button frame.
m_ToolbarBox.append(m_ButtonSave);           // Add button to the toolbar box.

// TEMPORARY WORKAROUND for broken GTKmm4 headers on your system:
// This uses the GTKmm3-like approach for loading image from resource.
// Create an empty Gtk::Image object first:
Gtk::Image svg_image_obj;
// Then call the member function set_from_resource on that object:
svg_image_obj.set_from_resource("/org/gtkmm/example/my_icon.svg");
// And then set it as the child of your button:
m_ButtonSVG.set_child(svg_image_obj);

// Configure the custom SVG button.
m_ButtonSVG.set_tooltip_text("My Custom SVG Icon"); // Set tooltip text.
m_ButtonSVG.set_has_frame(false);                   // Remove button frame.
m_ToolbarBox.append(m_ButtonSVG);                   // Add button to the toolbar box.

// Connect button signals to their respective handler functions.
m_ButtonNew.signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_button_new_clicked));
m_ButtonOpen.signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_button_open_clicked));
m_ButtonSave.signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_button_save_clicked));
m_ButtonSVG.signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_button_svg_clicked));

// Add the toolbar box and the label to the main vertical box.
m_VBox.append(m_ToolbarBox);
m_VBox.append(m_Label);
// Set top margin for the label.
m_Label.set_margin_top(10);
// Center the label horizontally.
m_Label.set_halign(Gtk::Align::CENTER);

}

// MyWindow destructor implementation.
MyWindow::~MyWindow() {}

// Handler for the “New File” button click.
void MyWindow::on_button_new_clicked()
{
m_Label.set_text(“You clicked ‘New File’.”); // Update label text.
std::cout << “New File clicked!” << std::endl; // Print to console.
}

// Handler for the “Open File” button click.
void MyWindow::on_button_open_clicked()
{
m_Label.set_text(“You clicked ‘Open File’.”); // Update label text.
std::cout << “Open File clicked!” << std::endl; // Print to console.
}

// Handler for the “Save File” button click.
void MyWindow::on_button_save_clicked()
{
m_Label.set_text(“You clicked ‘Save File’.”); // Update label text.
std::cout << “Save File clicked!” << std::endl; // Print to console.
}

// Handler for the custom SVG icon button click.
void MyWindow::on_button_svg_clicked()
{
m_Label.set_text(“You clicked the custom SVG icon.”); // Update label text.
std::cout << “SVG icon clicked!” << std::endl; // Print to console.
}

// Main function of the application.
int main(int argc, char* argv)
{
// Register application resources (e.g., embedded icons).
g_resources_register(resources_get_resource());

// Create a new Gtk::Application instance.
auto app = Gtk::Application::create("org.gtkmm.example.ToolbarIcons", Gio::Application::Flags::NONE);
// Run the application, creating and showing MyWindow.
return app->make_window_and_run<MyWindow>(argc, argv);

}

but this not work:

#include <gtkmm.h>
#include

// External function to get the GResource for the application.
extern GResource *resources_get_resource(void);

// MyWindow class definition, inheriting from Gtk::Window.
class MyWindow : public Gtk::Window
{
public:
// Constructor.
MyWindow();
// Destructor, overriding the base class destructor.
~MyWindow() override;

protected:
// Signal handlers for button clicks.
void on_button_new_clicked();
void on_button_open_clicked();
void on_button_save_clicked();
void on_button_svg_clicked();

// Member widgets for the window layout and controls.
Gtk::Box m_VBox;           // Vertical box to arrange widgets.
Gtk::Box m_ToolbarBox;     // Horizontal box for toolbar buttons.
Gtk::Button m_ButtonNew;    // Button for "New File".
Gtk::Button m_ButtonOpen;   // Button for "Open File".
Gtk::Button m_ButtonSave;   // Button for "Save File".
Gtk::Button m_ButtonSVG;    // Button for a custom SVG icon.
Gtk::Label m_Label;        // Label to display messages.

};

// MyWindow constructor implementation.
MyWindow::MyWindow()
: m_VBox(Gtk::Orientation::VERTICAL), // Initialize vertical box.
m_ToolbarBox(Gtk::Orientation::HORIZONTAL), // Initialize horizontal toolbar box.
m_Label(“Click a button on the toolbar.”) // Initial text for the label.
{
// Set the window title.
set_title(“Toolbar with Icons Example (Fixed for gtkmm4)”);
// Set the default size of the window.
set_default_size(400, 200);
// Set the main vertical box as the child of the window.
set_child(m_VBox);

// Configure the "New" button.
m_ButtonNew.set_icon_name("document-new"); // Use a standard icon.
m_ButtonNew.set_tooltip_text("New File");  // Set tooltip text.
m_ButtonNew.set_has_frame(false);          // Remove button frame for a toolbar look.
m_ToolbarBox.append(m_ButtonNew);          // Add button to the toolbar box.

// Configure the "Open" button.
m_ButtonOpen.set_icon_name("document-open"); // Use a standard icon.
m_ButtonOpen.set_tooltip_text("Open File");  // Set tooltip text.
m_ButtonOpen.set_has_frame(false);           // Remove button frame.
m_ToolbarBox.append(m_ButtonOpen);           // Add button to the toolbar box.

// Configure the "Save" button.
m_ButtonSave.set_icon_name("document-save"); // Use a standard icon.
m_ButtonSave.set_tooltip_text("Save File");  // Set tooltip text.
m_ButtonSave.set_has_frame(false);           // Remove button frame.
m_ToolbarBox.append(m_ButtonSave);           // Add button to the toolbar box.

// Load custom SVG icon from resource using create_from_resource.
// This is the preferred GTKmm4 way if headers are working correctly.
m_ButtonSVG.set_child(*Gtk::Image::create_from_resource("/org/gtkmm/example/my_icon.svg"));

// Configure the custom SVG button.
m_ButtonSVG.set_tooltip_text("My Custom SVG Icon"); // Set tooltip text.
m_ButtonSVG.set_has_frame(false);                   // Remove button frame.
m_ToolbarBox.append(m_ButtonSVG);                   // Add button to the toolbar box.

// Connect button signals to their respective handler functions.
m_ButtonNew.signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_button_new_clicked));
m_ButtonOpen.signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_button_open_clicked));
m_ButtonSave.signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_button_save_clicked));
m_ButtonSVG.signal_clicked().connect(sigc::mem_fun(*this, &MyWindow::on_button_svg_clicked));

// Add the toolbar box and the label to the main vertical box.
m_VBox.append(m_ToolbarBox);
m_VBox.append(m_Label);
// Set top margin for the label.
m_Label.set_margin_top(10);
// Center the label horizontally.
m_Label.set_halign(Gtk::Align::CENTER);

}

// MyWindow destructor implementation.
MyWindow::~MyWindow() {}

// Handler for the “New File” button click.
void MyWindow::on_button_new_clicked()
{
m_Label.set_text(“You clicked ‘New File’.”); // Update label text.
std::cout << “New File clicked!” << std::endl; // Print to console.
}

// Handler for the “Open File” button click.
void MyWindow::on_button_open_clicked()
{
m_Label.set_text(“You clicked ‘Open File’.”); // Update label text.
std::cout << “Open File clicked!” << std::endl; // Print to console.
}

// Handler for the “Save File” button click.
void MyWindow::on_button_save_clicked()
{
m_Label.set_text(“You clicked ‘Save File’.”); // Update label text.
std::cout << “Save File clicked!” << std::endl; // Print to console.
}

// Handler for the custom SVG icon button click.
void MyWindow::on_button_svg_clicked()
{
m_Label.set_text(“You clicked the custom SVG icon.”); // Update label text.
std::cout << “SVG icon clicked!” << std::endl; // Print to console.
}

// Main function of the application.
int main(int argc, char* argv)
{
// Register application resources (e.g., embedded icons).
g_resources_register(resources_get_resource());

// Create a new Gtk::Application instance.
auto app = Gtk::Application::create("org.gtkmm.example.ToolbarIcons", Gio::Application::Flags::NONE);
// Run the application, creating and showing MyWindow.
return app->make_window_and_run<MyWindow>(argc, argv);

}
with this error:
g++ -std=c++17 main.cc resource.c -o app $(pkg-config --cflags --libs gtkmm-4.0)
main.cc: In constructor ‘MyWindow::MyWindow()’:
main.cc:66:40: error: ‘create_from_resource’ is not a member of ‘Gtk::Image’
66 | m_ButtonSVG.set_child(*Gtk::Image::create_from_resource(“/org/gtkmm/example/my_icon.svg”));
| ^~~~~~~~~~~~~~~~~~~~

what can be wrog? Gemini say that I have wrong header files:

"Based on the error message error: ‘create_from_resource’ is not a member of ‘Gtk::Image’, it’s not that a header file is ‘wrong’, but rather it indicates that the create_from_resource function is not declared or available in the Gtk::Image class definition that your compiler is seeing.

This can happen for several reasons, but most commonly it’s related to:

  1. GTKmm Version: Although the code is labeled as “Fixed for gtkmm4”, it’s possible that you are compiling with a different version of gtkmm (e.g., an older one) where this specific static method Gtk::Image::create_from_resource either doesn’t exist, has a different signature, or was introduced in a newer minor version of GTKmm4 than what you have installed.
  2. Incomplete Installation or Corrupted Headers: As the original comment in the code suggests (“TEMPORARY WORKAROUND for broken GTKmm4 headers on your system”), it’s possible that your installation of GTKmm4 development files is incomplete or some headers are corrupted, and therefore this function is not properly exported or recognized.

In summary, the problem isn’t that you have a wrong header file, but that the Gtk::Image class definition in your installed gtkmm-4.0 headers does not contain the create_from_resource method, leading to the compilation error.

Therefore, we reverted to the workaround that uses Gtk::Image svg_image_obj; svg_image_obj.set_from_resource(...), as that appears to work on your system."

Is it possible?

Yeah, gtkmm doesn’t have Gtk::Image::create_from_resource, see: gtk/src/image.hg · 4.18.0 · GNOME / gtkmm · GitLab

So either default-construct a Gtk::Image and call set_from_resource:

Gtk::Image image;
image.set_from_resource("/path/my/resource");

Or use the C API and wrap via gtkmm:

Gtk::Image& image = *Gtk::Image::wrap(gtk_image_new_from_resource ("/path/my/resource"));

In short; there’s nothing wrong, it’s just a design choice in gtkmm

1 Like