CodexBloom - Programming Q&A Platform

QML GridView not rendering items correctly when using custom delegate in Qt 6.5

👀 Views: 29 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
Qt QML GridView qml

I'm optimizing some code but I'm having trouble with I'm a bit lost with I'm having an scenario with a `GridView` in QML where the items are not being rendered correctly when I apply a custom delegate. I'm using Qt 6.5 and expecting each item to display a specific layout, but instead, I'm seeing overlapping items and unexpected spacing. Here's an example of how I'm currently setting it up: ```qml GridView { id: gridView width: 400 height: 300 cellWidth: 100 cellHeight: 100 model: myModel delegate: Item { width: 100 height: 100 Rectangle { anchors.fill: parent color: "lightblue" Text { anchors.centerIn: parent text: modelData.name } } } } ``` The model, `myModel`, is a simple list of objects, each containing a `name` property. When I run this code, some of the items overlap, and it seems like the `GridView` is not respecting the `cellWidth` and `cellHeight` properties. I've tried adjusting those values and setting `spacing`, but that hasn't resolved the scenario. Another thing I noticed is that if I remove the custom delegate and use a basic `Text` element directly in the `GridView`, it displays correctly without any overlaps. I'm not seeing any behavior messages in the console, so I suspect it might be related to how the delegate is defined or the model data it receives. Any thoughts on what might be going wrong here? I'm looking for best practices on using `GridView` with custom delegates, especially regarding item layout and sizing. Is there a simpler solution I'm overlooking? I'm using Qml latest in this project. I'm coming from a different tech stack and learning Qml.