Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-93293

QML: Initial setting of ListView currentIndex is ignored with dynamic model generation

    XMLWordPrintable

Details

    • Linux/X11

    Description

      Initial setting of ListView currentIndex is ignored with dynamic model generation.

      Please see attached test qml file: listview_test4.qml.

      In this test code ListView.model was dynamically generated before ListView.onCompleted.

      But default setting of currentIndex (at line 49) does not work.

      With this test code, it seems that QQuickItemViewChangeSet::applyChanges() in qquickitemview.cpp misbehaves.

      void QQuickItemViewChangeSet::applyChanges(const QQmlChangeSet &changeSet)
      {
          pendingChanges.apply(changeSet);
      
          int moveId = -1;
          int moveOffset = 0;
      
          for (const QQmlChangeSet::Change &r : changeSet.removes()) {
              itemCount -= r.count;
              if (moveId == -1 && newCurrentIndex >= r.index + r.count) {
                  newCurrentIndex -= r.count;
                  currentChanged = true;
              } else if (moveId == -1 && newCurrentIndex >= r.index && newCurrentIndex < r.index + r.count) {
                  // current item has been removed.
                  if (r.isMove()) {
                      moveId = r.moveId;
                      moveOffset = newCurrentIndex - r.index;
                  } else {
                      currentRemoved = true;
                      newCurrentIndex = -1;
                      if (itemCount)
                          newCurrentIndex = qMin(r.index, itemCount - 1);
                  }
                  currentChanged = true;
              }
          }
          for (const QQmlChangeSet::Change &i : changeSet.inserts()) {
              if (moveId == -1) {
                  if (itemCount && newCurrentIndex >= i.index) {
                      newCurrentIndex += i.count;
                      currentChanged = true;
                  } else if (newCurrentIndex < 0) {
                      newCurrentIndex = 0;
                      currentChanged = true;
                  } else if (newCurrentIndex == 0 && !itemCount) {
                      // this is the first item, set the initial current index
                      currentChanged = true;
                  }
              } else if (moveId == i.moveId) {
                  newCurrentIndex = i.index + moveOffset;
              }
              itemCount += i.count;
          }
      } 

      The initial value of newCurrentIndex of above function is 1 (it's from initial setting of currentIndex: 1 at line 49 in listview_test4.qml) and this function was called 5 times (the number of model items) before LIstView.onCompleted.

      This results in wrong currentIndex of ListView.

      The only workaround is setting initial currentIndex at ListView.onComplted. (see the line 50 in listview_test4.qml)

       

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              fabiankosmale Fabian Kosmale
              seokhako Seokha Ko
              Votes:
              0 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes