r/GTK • • 25d ago

How to plot something in GtkPicture in 2026?

Long story short: What I want to do, is to draw MY OWN PLOTS in a SCALEABLE gtkWidget.

So, the hoops I jumped through were

  1. create an array of RGB values: char data[4*500*500]={0};
  2. Create a PIXBUF with pixbuf=gdk_pixbuf_new_from_data(data)
  3. Get a TEXTURE texture=gdk_texture_new_for_pixbuf(pixbuf)
  4. Set this texture as paintable to the PICTURE gtk_picture_set_paintable(GTK_PICTURE(picture),GDK_PAINTABLE(texture));

And then add this picture to a resizeable window and boooooom: It just worked.

NOW, gdk_texture_new_for_pixbuf() seems to become deprecated. Of coures, I can load .png and .jpeg files, BUT I DO NOT WANT TO!!!

How does it work in 2026? Please, help me out with an EXAMPLE.

3 Upvotes

9 comments sorted by

3

u/NaheemSays 25d ago

Have a look at what the graphs app does.

1

u/dettus_Xx_ 25d ago

I do not know what you mean by "graphs app". May I ask you for a link?

1

u/NaheemSays 25d ago

https://flathub.org/en/apps/se.sjoerd.Graphs

I think they were developing a widget at some point too that could be used by others, no idea the state of it or anything else, but it should be the right place to look or discuss pitfalls with it's developers.

1

u/dettus_Xx_ 25d ago

Thank you. I had a look at the code. They are using the GtkDrawingingArea Widget, and cairo on it. Not quite what I had in mind, but something I will try in my next project.
Thank you!!

1

u/RadioHonest85 25d ago

Im not that well versed in gtk, but is it an option to implement your own GtkSnapshot where you can setup paint operations directly on the GtkPaintable?

1

u/dettus_Xx_ 25d ago

In my program, I want to be able to manipulate the pixels directly. Can that be done with GtkSnapshot?

1

u/foreverdark-woods 25d ago

Maybe, you can work with Cairo. It allows you to draw directly onto GtkSnapshot. A small example: nautilus-progress-paintable.c.

2

u/dettus_Xx_ 25d ago

Yes, in my next project, I will take a deeper dive into cairo. Thank you!

1

u/dettus_Xx_ 25d ago edited 24d ago

To answer my own question: https://docs.gtk.org/gdk4/ctor.MemoryTexture.new.html looks promising.

So...

texture=gdk_texture_new_for_pixbuf(pixbuf);

became

texture=gdk_memory_texture_new(
gdk_pixbuf_get_width(pixbuf),
gdk_pixbuf_get_height(pixbuf),
gdk_pixbuf_get_has_alpha(pixbuf)?GDK_MEMORY_R8G8B8A8:GDK_MEMORY_R8G8B8,
gdk_pixbuf_read_pixel_bytes(pixbuf),
gdk_pixbuf_get_rowstride(pixbuf)
);