CodexBloom - Programming Q&A Platform

QML Gallery Item not Resizing Correctly in Qt 6.5 with Dynamic Content

👀 Views: 37 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
Qt QML dynamic-content qml

I'm prototyping a solution and I'm working through a tutorial and I'm facing an issue where a QML Gallery item is not resizing correctly when its dynamic content changes. I'm using Qt 6.5 and attempting to display images fetched from a web service. The images load asynchronously, but the gallery items seem to maintain their initial sizes and do not resize to fit the actual image dimensions after loading. I'm using an Image element inside a Rectangle as shown below: ```qml Gallery { id: imageGallery width: parent.width height: 300 model: ListModel { id: imageModel ListElement { imageSource: "https://example.com/image1.jpg" } ListElement { imageSource: "https://example.com/image2.jpg" } } delegate: Item { width: 200 height: 200 Rectangle { id: imgContainer anchors.fill: parent Image { id: galleryImage source: model.imageSource fillMode: Image.PreserveAspectFit onLoaded: { imgContainer.width = galleryImage.width imgContainer.height = galleryImage.height } } } } } ``` I've tried binding the width and height of the `Rectangle` containing the `Image` to the `Image`'s dimensions, but that does not seem to work as expected. I also attempted using `Component.onCompleted` to trigger a resize function, but it still doesn't update dynamically when the content changes. Additionally, I'm seeing a flickering effect when the images load, which might be related to how the items are being rendered. I have verified that the image URLs are correct, and the images do load successfully, just not with the expected resizing behavior. Any guidance on how to ensure the gallery items adapt to the loaded content would be greatly appreciated. For context: I'm using Qml on Ubuntu 22.04. What are your experiences with this? This is happening in both development and production on macOS. Thanks in advance! I'm on CentOS using the latest version of Qml. I'm open to any suggestions.