GridView, how to position the view at startup?
-
Hello everyone,
I do not know how to set the position of the view when the Gridview becomes visible.
As an example, the following code.qml:
import QtQuick.Layouts import QtQuick.Controls import QtQuick import QtQml ColumnLayout { RowLayout { Layout.fillWidth: true SpinBox { id:spin value: 0 from:0 to:yearSelector.count-1 onValueModified: { yearSelector.positionViewAtIndex(spin.value, GridView.Beginning) } } } GridView { id:yearSelector Layout.fillHeight: true Layout.fillWidth: true model: ListModel { ListElement {year:0} ListElement {year:1} ListElement {year:2} ListElement {year:3} ListElement {year:4} ListElement {year:5} ListElement {year:6} ListElement {year:7} ListElement {year:8} ListElement {year:9} ListElement {year:10} ListElement {year:11} ListElement {year:12} ListElement {year:13} ListElement {year:14} ListElement {year:15} ListElement {year:16} ListElement {year:17} ListElement {year:18} ListElement {year:19} } cellWidth:yearSelector.width*0.3333 cellHeight:yearSelector.height*0.5 //currentIndex :15 Component.onCompleted: yearSelector.positionViewAtIndex(15, GridView.Beginning) delegate:Button { text:year } } }
shows a GridView with 20 Buttons. The grid view has 3 columns and 7 rows and shows 2 rows at a time.
I was thinking that setting
currentIndex :15
will show in the view the button with text=15
at startup, but this does not work.
Also, when callingComponent.onCompleted: yearSelector.positionViewAtIndex(15, GridView.Beginning)
does not show the button with text =15
at startup.If I change the spinBox value to 15 the GridView shows the button with text =
15
.I am testing the type like
~/Qt/6.7.0/gcc_64/bin/qml code.qml
Is this a bug in GridView or i am doing something wrong?
Many thanks for your time. -
With a few minor changes to give the window a reasonable size and to some sort of rendering issue, the view starts with index 15 in the upper left corner on my macOS, Qt 6.5.3 installation. This may be a recently introduced bug.
Does the GridView.count property reflect the expected value?
-
if I do
Component.onCompleted: { console.log(yearSelector.count); yearSelector.positionViewAtIndex(15, GridView.Beginning) }
It prints 20 as expected;
Also, I tried with~/Qt/6.5.3/gcc_64/bin/qml code.qml
and got the same results, what did you change? -
import QtQuick.Layouts import QtQuick.Controls import QtQuick ColumnLayout { width: 300 height: 600 SpinBox { from:0 to:yearSelector.count-1 onValueModified: yearSelector.positionViewAtIndex(value, GridView.Beginning) } GridView { id:yearSelector Layout.fillHeight: true Layout.fillWidth: true model: 20 cellWidth: width / 3 cellHeight: height / 2 Component.onCompleted: yearSelector.positionViewAtIndex(15, GridView.Beginning) delegate: Text { text: index } } }
My setup (hardware, OS, applications, etc) seems to be particularly sensitive to contrast changes, leading to unreadable highlights and text in some applications. I don't think that the changes mentioned above are altering the behavior, but it's worth trying.