I am trying to create a Gtk::Grid containing Gtk::Pictures. I would like to be able to scroll through this grid of pictures so that I can select them and perform actions on them. I had a gtkmm3 GUI that could do this using a Gtk::ScrolledWindow, Gtk::Viewport, and Gtk::Grid. I used Glade to design that GUI and did not have to do much in C++ to get the desired effect. Now, I’m in GTK4 and creating these widgets in source code and what I am seeing is that the grid resizes the images when the number of rows would cause it to adjust the scrollbar so that all images are displayed at once.
Below is a code snippet of how the Gtk::Frame, Gtk::ScrolledWindow, and Gtk::Grid are built. I removed the Gtk::Viewport because the Gtk::ScrolledWindow documentation suggested one would be created for me (I also tested creating one myself).
That example does not contain images. It probably does not solve the unwanted resizing.
I’m not sure what to do here. Are your images stored in Gtk::Image objects?
You can try calling Gtk::Widget::set_size_request() on the images.
I’ve put images in a scrollable grid view in Python. I used a box for the grid entries so that I could include a text label with each image.
I hit issues with my first implementation just adding the fixed sized images to the box and then adding the boxes to the grid. Once I had several thousand entries (big appreciation for GridView supporting this nicely), I started to see issues scrolling a long way - the images would grow, there would be fewer entries per grid row and sometimes large white space gaps between rows.
In the end I looked at how Gnome Files worked (the nautilus-grid-* files in src · main · GNOME / Files · GitLab) It uses Bin and Clamp in the grid entry objects in ways I don’t understand but copying that approach solved my issues with the grid.
You can try Cambalache which is the Glade equivalent for GTK4 by the same developer. It’s still in the works, but should be good enough for most common use cases.
Thanks for the replies, everyone. After going through some of your comments, I noticed that I was using set_can_shrink(false) for my main Gtk::Picture, but not the ones in the grid. Once I set that to false for the grid entries, the total size exceeded the allocation for the Gtk::ScrolledWindow and the scroll bars activated. Problem solved!