Add defaultCurrentIndex to H(List/Grid)View

When the model of a view changes, Qt automatically sets the
currentIndex to 0.
Reset it back to the set default (usually -1) instead.
This commit is contained in:
miruka 2020-09-02 14:40:06 -04:00
parent 3fb64dbef5
commit 509c4a0871
2 changed files with 8 additions and 2 deletions

View File

@ -6,6 +6,8 @@ import QtQuick.Controls 2.12
GridView { GridView {
id: gridView id: gridView
property int defaultCurrentIndex: -1
property alias cursorShape: mouseArea.cursorShape property alias cursorShape: mouseArea.cursorShape
property int currentItemHeight: currentItem ? currentItem.height : 0 property int currentItemHeight: currentItem ? currentItem.height : 0
@ -65,7 +67,7 @@ GridView {
} }
currentIndex: -1 currentIndex: defaultCurrentIndex
keyNavigationWraps: true keyNavigationWraps: true
highlightMoveDuration: theme.animationDuration highlightMoveDuration: theme.animationDuration
@ -118,6 +120,7 @@ GridView {
} }
onSelectedCountChanged: if (! selectedCount) lastCheckedDelegateIndex = 0 onSelectedCountChanged: if (! selectedCount) lastCheckedDelegateIndex = 0
onModelChanged: currentIndex = defaultCurrentIndex
HKineticScrollingDisabler { HKineticScrollingDisabler {
id: mouseArea id: mouseArea

View File

@ -6,6 +6,8 @@ import QtQuick.Controls 2.12
ListView { ListView {
id: listView id: listView
property int defaultCurrentIndex: -1
property int currentItemHeight: currentItem ? currentItem.height : 0 property int currentItemHeight: currentItem ? currentItem.height : 0
property var checked: ({}) property var checked: ({})
@ -76,7 +78,7 @@ ListView {
} }
currentIndex: -1 currentIndex: defaultCurrentIndex
keyNavigationWraps: true keyNavigationWraps: true
highlightMoveDuration: theme.animationDuration highlightMoveDuration: theme.animationDuration
highlightResizeDuration: theme.animationDuration highlightResizeDuration: theme.animationDuration
@ -135,6 +137,7 @@ ListView {
} }
onSelectedCountChanged: if (! selectedCount) lastCheckedDelegateIndex = 0 onSelectedCountChanged: if (! selectedCount) lastCheckedDelegateIndex = 0
onModelChanged: currentIndex = defaultCurrentIndex
HKineticScrollingDisabler { HKineticScrollingDisabler {
width: enabled ? parent.width : 0 width: enabled ? parent.width : 0