Hi folks,
I’m working on a GTK4 app in Rust and I’m hitting a layout issue with Pictureinside a ListView item. The goal is to display dynamically generated preview images in a scrolling list and have each image:
- Scale down to fit the width of the viewport (if needed)
- Maintain its aspect ratio
- Automatically size its container to fit its expected height
But what I’m seeing is:
- Each picture has zero height unless I explicitly set it manually in code. Without calling
set_size_requeston theBoxcontaining thePicture, the image doesn’t visibly render at all.
Here’s the relevant code from my SignalListItemFactory setup:
factory.connect_setup(|_, list_item| {
let picture = gtk::Picture::new();
picture.set_content_fit(gtk::ContentFit::Contain);
picture.set_halign(gtk::Align::Center);
picture.set_valign(gtk::Align::Start);
picture.set_hexpand(true);
picture.set_vexpand(true);
let container = gtk::Box::new(gtk::Orientation::Vertical, 0);
container.set_halign(gtk::Align::Center);
container.set_valign(gtk::Align::Start);
container.set_hexpand(true);
container.set_vexpand(true);
container.append(&picture);
list_item.set_child(Some(&container));
});
And then during bind:
if let Some(texture) = page.texture() {
picture.set_paintable(Some(&texture));
// Without this, picture has zero height
let height = texture.height();
container.set_size_request(-1, height as i32);
}
I’d like the list item to automatically size itself vertically to match the loaded picture with no hardcoded height (as this doesn’t scale down in narrow viewports). I expected Picture or the container Box to request the correct size based on the image, but that doesn’t happen.
Questions:
- Is this the expected behavior for
Pictureinside a list item? - What’s the correct way to size list items dynamically based on picture height?
- Should I be using a
Frame,AspectFrame, or something else to let GTK size to the texture?
Any help is much appreciated!
Thanks!