<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[SOLVED; With one qualification(see thread)] GridView Problem with *example*]]></title><description><![CDATA[<p dir="auto">Hi,<br />
So my powers of description for the problem I am having were not sufficient to inspire any solutions :-). Let me  show a simple example.</p>
<p dir="auto">The problem: GridView Items do not come into view from off screen when changing their position in the grid.</p>
<p dir="auto">Example: This example illustrates how highlighting items in the grid using the arrow keys will bring off screen items into view but not when moving items. By default the grid is setup to highlight items. By pressing the "enter" key it switches modes to move the current item to the position indicated by the arrow key pressed. In the "move" mode the current item is highlighted in yellow and in the "highlight" mode it is highlighted in steel blue.</p>
<p dir="auto">Notice that when in "move" mode the off screen items do not come into view. You can toggle between the two modes to see what I mean by hitting the "enter" key.</p>
<p dir="auto">The currentIndex documentation for GridView says ...</p>
<p dir="auto">bq.  The currentIndex property holds the index of the current item, and currentItem holds the current item. Setting the currentIndex to -1 will clear the highlight and set currentItem to null.<br />
If highlightFollowsCurrentItem is true, setting either of these properties will smoothly scroll the GridView so that the current item becomes visible.</p>
<p dir="auto">Since the highlight follows the current index by default, the index is clearly changing so the item should come into view. Any ideas?</p>
<p dir="auto">@<br />
import QtQuick 2.3<br />
import QtQuick.Controls 1.2</p>
<p dir="auto">Rectangle {<br />
id: mainview<br />
width: 700<br />
height: 700<br />
color: "black"</p>
<pre><code>ListModel {
    id: testModel

    ListElement {
            name: "one"
            colorName: "red"
    }
    ListElement {
            name: "two"
            colorName: "green"
    }
    ListElement {
            name: "three"
            colorName: "blue"
    }
    ListElement {
            name: "four"
            colorName: "orange"
    }
    ListElement {
            name: "five"
            colorName: "cyan"
    }
    ListElement {
            name: "six"
            colorName: "white"
    }
    ListElement {
            name: "seven"
            colorName: "grey"
    }
    ListElement {
            name: "eight"
            colorName: "magenta"
    }
    ListElement {
            name: "nine"
            colorName: "darkred"
    }
    ListElement {
            name: "seven"
            colorName: "darkgreen"
    }
    ListElement {
            name: "eight"
            colorName: "darkblue"
    }
    ListElement {
            name: "nine"
            colorName: "darkmagenta"
    }

}


Component {
    id: highlightBar

    Rectangle {
        id: highlightRect

        color: "lightSteelBlue"
        width: view.cellWidth; height: view.cellHeight
    }
}


GridView {
    id: view
    height: parent.height
    width: 225*3
    cellWidth: 225; cellHeight: 225
    anchors.horizontalCenter: parent.horizontalCenter
    model: testModel
    highlight: highlightBar
    highlightMoveDuration: 200
    property string mode : "highlight"
    focus: true

    delegate: Rectangle{
                    width: view.cellWidth
                    height: view.cellHeight
                    color: "transparent"

                    Rectangle {
                        anchors.centerIn: parent
                        width: 200
                        height: 200
                        color: colorName
                    }
              }

    move: Transition {
             NumberAnimation { properties: "x,y"; duration: 400; easing.type: Easing.OutBounce }
        }

    displaced: Transition {
             NumberAnimation { properties: "x,y"; duration: 400; easing.type: Easing.OutBounce }
        }


    Keys.onRightPressed:  {
        if (mode == "move") {
            testModel.move(view.currentIndex,view.currentIndex+1,1);
        }else{
            view.moveCurrentIndexRight();
        }
    }

    Keys.onLeftPressed:  {
        if (mode == "move") {
            testModel.move(view.currentIndex,view.currentIndex-1,1);
        }else{
            view.moveCurrentIndexLeft();
        }
    }

    Keys.onUpPressed:  {
        if (mode == "move") {
            model.move(view.currentIndex,view.currentIndex - 3,1);
        }else{
            view.moveCurrentIndexUp();

        }
    }

    Keys.onDownPressed:  {
        if (mode == "move") {
            model.move(view.currentIndex,view.currentIndex + 3,1);
        }else{
            view.moveCurrentIndexDown();
        }
    }


    Keys.onReturnPressed: {
        if (mode == "move") {
            highlightItem.color = "lightSteelBlue";
            highlightItem.opacity = .4;
            view.highlightMoveDuration = 200;
            mode = "highlight";
        }else{
            highlightItem.color = "yellow";
            highlightItem.opacity = 1.0;
            view.highlightMoveDuration = 0;
            mode = "move";
        }

    }

}
</code></pre>
<p dir="auto">}</p>
<p dir="auto">@</p>
]]></description><link>https://forum.qt.io/topic/43802/solved-with-one-qualification-see-thread-gridview-problem-with-example</link><generator>RSS for Node</generator><lastBuildDate>Fri, 13 Nov 2026 01:50:28 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/43802.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 17 Jul 2014 19:08:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED; With one qualification(see thread)] GridView Problem with *example* on Fri, 18 Jul 2014 16:49:56 GMT]]></title><description><![CDATA[<p dir="auto">That's Good. Thanks for the Info. Added to my Lib. :)</p>
]]></description><link>https://forum.qt.io/post/236338</link><guid isPermaLink="true">https://forum.qt.io/post/236338</guid><dc:creator><![CDATA[p3c0]]></dc:creator><pubDate>Fri, 18 Jul 2014 16:49:56 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED; With one qualification(see thread)] GridView Problem with *example* on Fri, 18 Jul 2014 15:38:17 GMT]]></title><description><![CDATA[<p dir="auto">FYI ... This worked fine until I tried to scale it up to around twenty items in the grid. Then when I tried hitting the left key repeatedly to move an item from the bottom of the grid to the top I ran into a problem where the screen would periodically go completely black for no reason.</p>
<p dir="auto">Turns out I needed to set the GridView's cacheBuffer property to an appropriately high number so that enough delegates were retained outside the visible area of the view. After I did that it worked fine.</p>
<p dir="auto">Thanks again</p>
]]></description><link>https://forum.qt.io/post/236331</link><guid isPermaLink="true">https://forum.qt.io/post/236331</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 18 Jul 2014 15:38:17 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED; With one qualification(see thread)] GridView Problem with *example* on Fri, 18 Jul 2014 15:02:35 GMT]]></title><description><![CDATA[<p dir="auto">Hey thanks, that did the trick. :-)</p>
]]></description><link>https://forum.qt.io/post/236328</link><guid isPermaLink="true">https://forum.qt.io/post/236328</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 18 Jul 2014 15:02:35 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED; With one qualification(see thread)] GridView Problem with *example* on Fri, 18 Jul 2014 06:52:55 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">It seems move doesn't set the currentIndex. You can set the currentIndex manually and then use "positionViewAtIndex":<a href="http://qt-project.org/doc/qt-5/qml-qtquick-gridview.html#positionViewAtIndex-method" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-5/qml-qtquick-gridview.html#positionViewAtIndex-method</a> to make the item visible properly.<br />
For eg. I modified your example,<br />
@<br />
Keys.onUpPressed:  {<br />
if (mode == "move") {<br />
model.move(view.currentIndex,view.currentIndex - 3,1);<br />
currentIndex = currentIndex - 3<br />
positionViewAtIndex(currentIndex,GridView.Contain)<br />
}else{<br />
view.moveCurrentIndexUp();<br />
}<br />
}</p>
<p dir="auto">Keys.onDownPressed:  {<br />
if (mode == "move") {<br />
model.move(currentIndex,currentIndex + 3,1);<br />
currentIndex = currentIndex + 3<br />
positionViewAtIndex(currentIndex,GridView.Contain)<br />
}else{<br />
view.moveCurrentIndexDown();<br />
}<br />
}<br />
@</p>
]]></description><link>https://forum.qt.io/post/236266</link><guid isPermaLink="true">https://forum.qt.io/post/236266</guid><dc:creator><![CDATA[p3c0]]></dc:creator><pubDate>Fri, 18 Jul 2014 06:52:55 GMT</pubDate></item></channel></rss>