[Interest] [SOLVED] ListView not start at index 0 when is Horizontal

Gianluca gmaxera at gmail.com
Mon Apr 6 19:03:42 CEST 2015


Hello Federico,
thank to point where the problem appear.
It seems a nasty bug.
However, I cannot solve it using Layouts because I’m not comfortable on using Layouts. I prefer to use anchors.
In any case, I solved the problem.
In fact, the delegate Item has the width set has the parent.width (that it is the ListView behaving oddly).
When I removed this binding, the odd behavior disappeared and I can constraint the width and the height as I want (and I prefer :-) )

Thanks,
Gianluca.


Il giorno 06/apr/2015, alle ore 18:28, Federico Buti <bacarozzo at gmail.com> ha scritto:

> Hi Gianluca,
> it's hard to say if it is a bug or not. The problem is anyway related to the sizing of the list, i.e. if you set listHistory.width to
> 
> width: parent.width
> 
> the problem disappears. If you set a similar constraint for the height and set the ListView to the vertical layout the same, odd, behaviour occurs.
> 
> Instead of using such constraints, what about using a layout to size your three elements? Something involving a RowLayout, for instance, works perfectly. See the modified code below:
> 
> import QtQuick 2.4
> import QtQuick.Window 2.2
> import QtQuick.Layouts 1.1
> 
> Window {
>     visible: true
>     width: 400
>     height: 400
>     
>     RowLayout {
>         anchors.fill: parent
>         
>         Item {
>             id: previous
>             Layout.preferredWidth: parent.width / 3
>             Layout.fillHeight: true
>         }
>         
>         ListView {
>             id: listHistory
>             Layout.preferredWidth: parent.width / 3
>             Layout.fillHeight: true
>             clip: true
>             orientation: ListView.Horizontal
>             snapMode: ListView.SnapToItem
>             highlightRangeMode: ListView.StrictlyEnforceRange
>             onCurrentIndexChanged: console.log( "Current Index: "+currentIndex )
>             model: modelHistory
>             delegate: Item {
>                 width: listHistory.width
>                 height: listHistory.height
>                 Text {
>                     width: parent.width
>                     anchors.bottom: parent.verticalCenter
>                     
>                     font.pixelSize: 20 //* mm
>                     minimumPixelSize: 10 //* mm
>                     fontSizeMode: Text.Fit
>                     horizontalAlignment: Text.AlignHCenter
>                     text: year
>                 }
>                 Text {
>                     
>                     width: parent.width
>                     height: parent.height/2-1.5 //* mm
>                     anchors.bottom: parent.bottom
>                     font.pixelSize: 20 //* mm
>                     minimumPixelSize: 10 //* mm
>                     fontSizeMode: Text.Fit
>                     horizontalAlignment: Text.AlignHCenter
>                     wrapMode: Text.Wrap
>                     text: what
>                 }
>             }
>         }
>         
>         Item {
>             id: next
>             Layout.preferredWidth: parent.width / 3
>             Layout.fillHeight: true
>         }
>     }
> }
> 
> Cheers,
> F.
> 
> ---
> Federico Buti
> 
> On 6 April 2015 at 11:15, Gianluca <gmaxera at gmail.com> wrote:
> Dear all,
> I’m experiencing a unexpected behavior:
> I have a ListView in QML that display data from a ListModel.
> The dimension of ListView is set in such a way that only one element delegate is display at time.
> When I configure the ListView to be Vertical, then the first item displayed is the first item on the ListModel.
> But when I configure the ListView to be Horizontal, then the first item displayed is not the first item on the ListModel … but one in the middle.
> Why ??
> Forcing the property currentIndex to 0 do not solve the problem.
> Calling positionViewAtBeginning do not solve the problem :-(
> 
> What else I can do ?
> 
> // Code of ListView:
>                 ListView {
>                         id: listHistory
>                         anchors.centerIn: parent
>                         height: parent.height
>                         width: parent.width-previous.width-next.width
>                         clip: true
>                         orientation: ListView.Horizontal
>                         snapMode: ListView.SnapToItem
>                         highlightRangeMode: ListView.StrictlyEnforceRange
>                         onCurrentIndexChanged: console.log( "Current Index: "+currentIndex )
>                         model: modelHistory
>                         delegate: Item {
>                                 width: listHistory.width
>                                 height: listHistory.height
>                                 Text {
>                                         width: parent.width
>                                         anchors.bottom: parent.verticalCenter
>                                         color: "white"
>                                         font.pixelSize: 8.5*mm
>                                         minimumPixelSize: 4*mm
>                                         fontSizeMode: Text.Fit
>                                         horizontalAlignment: Text.AlignHCenter
>                                         text: year
>                                 }
>                                 Text {
>                                         color: "white"
>                                         width: parent.width
>                                         height: parent.height/2-1.5*mm
>                                         anchors.bottom: parent.bottom
>                                         font.pixelSize: 2.4*mm
>                                         minimumPixelSize: 1*mm
>                                         fontSizeMode: Text.Fit
>                                         horizontalAlignment: Text.AlignHCenter
>                                         wrapMode: Text.Wrap
>                                         text: what
>                                 }
>                         }
>                 }
> 
> // On the console I get the following message:
> qml: Current Index: 0
> qml: Current Index: 19
> 
> But Who change the current index to 19 ??
> 
> Thanks,
> Gianluca.
> 
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150406/245b245c/attachment.html>


More information about the Interest mailing list