How to save GTK4 ApplicationWindow to file

What is the recommended way to save a Window to file in GTK4?
Is there an example available which does this?

What kind of state are you trying to save? Could you describe what you want to achieve in a bit more detail?

I want to be able to save an image of the application main window with all the displayed widgets at any given time.
Something similar to print screen of my application’s window, but without using the OS print screen.
If at all possible I would like to allow this even when the application window is not the top window in the system.

May this help:

Taking a screenshot in GTK4 from pygobject, or Gsk support in pygobject

I am using the following code to override the snapshot_vfunc inorder to save a png from my application window:

void MainWindow::snapshot_vfunc(const Glib::RefPtrGtk::Snapshot& snapshot)
std::cout << “snapshot_vfunc()” << std::endl;

GskRenderer *renderer;
GskRenderNode *node;
GdkTexture *texture;

node = gtk_snapshot_to_node (snapshot->gobj()); // gtk_snapshot_free_to_node

GtkNative* pNative = this->get_native()->gobj();
renderer = gtk_native_get_renderer (pNative);
texture = gsk_renderer_render_texture (renderer, node, NULL);
gsk_render_node_unref (node);
gdk_texture_save_to_png(texture, "./png_file.png");

gsk_renderer_unrealize (renderer);
g_object_unref (texture);
g_object_unref (renderer);
g_object_unref (snapshot->gobj());


The PNG is saved correctly, however I get an assertion failure of size>0 when the function returns.
I also get a warning “Too many gtk_snapshot_push() calls. 3 states remaining” after calling gtk_snapshot_to_node.
How should I override this function to avoid these issues?

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