Qt Quick GridView variable cellWidth depending on Model is not working
-
My model is called ContactModel.qml with this code:
@
import QtQuick 2.2ListModel { ListElement { name: "Jim Williams" portrait: "item.png" } ListElement { name: "John Brown" portrait: "item.png" } ListElement { name: "Bill Smyth" portrait: "item.png" } }
@
and in my main.qml,@ GridView
{
width: 1360
height: 1120
cellWidth: width / ((ContactModel.count < 3) ? ContactModel.count: 3)
cellHeight: 386
model: ContactModel {}
delegate: Column
{
Image
{
source: portrait;
anchors.horizontalCenter: parent.horizontalCenter
}
Text
{
text: name;
anchors.horizontalCenter: parent.horizontalCenter
}
}
}
@Am I not allowed to use ternary operators/logic in qml files? I would like to have the width, height, and positioning vary depending on the amount of input. When I try to output ContactModel.count, I get nothing via messagebox, text, etc.
-
Hi,
You can definitely use the ternary operators. But the ternary operator expression you have used i think is wrong.
It should be something like this
@
cellWidth: model.count < 3 ? 250 : 120
//Replace 250 and 120 with what ever values you want or expressions that evaluate to a valid value.
@
Also where did you try to output model's count ?
An ideal way would be when Component's onCompleted function.
So in gridview,
@
Component.onCompleted: {
console.log(grid.model.count)
}
@will work.
Hope this helps. -
The problem is that the code is referring to ContactModel as if it was an instance, rather than a type. The error output from qmlscene gives this hint:
untitled1.qml:7: ReferenceError: ContactModel is not definedChanging the reference for the cellWidth binding from ContactModel.count to model.count works.
In anything beyond an example, the code should also be prepared for model being undefined, or count being 0.
-
I'm using Qt Creator. There didn't appear to be any errors before. Perhaps I haven't set up Qt Creator properly, since the design tab is also completely broken. Thanks for your help though!
-
There should be an "Application Output" box at the bottom of the Creator window. Clicking that opens a pane that displays the standard output and error streams from the application, as well as some execution information. It might not be open by default.