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

Missing count property change notification in ListView, GridView, PathView and Repeater

    XMLWordPrintable

Details

    • f09b12cea1143f1b2763064bb0d3e6592081de2b

    Description

      If the number of items in a model changes while the item view initialization is not completed, the view does not emit a countChanged notification.

      This can cause wrong evaluations in bound properties (e.g. if some text should be hidden if the listview is empty).

      This example is a stripped down version of actual code to illustrate the problem. The number of items is displayed as "3" whereas due to the filter it actually is 0.

      The bug exists in ListView, GridView, PathView and Repeater and can be fixed by emitting the countChanged signal in the itemsRemoved/itemsInserted slots of the views regardless of the view's component initialization state.

      The QtQuick 2.0 implementation in Qt 5.x does not have this problem.

      import QtQuick 1.1
      import org.test.models 1.0
      
      Rectangle {
        width: 360
        height: 360
      
        ListModel {
          id: m
          ListElement { filter: "X*" }
        }
      
        Repeater {
          model: m
          delegate: Column {
            property string mfilter: model.filter
            Text {
              visible: lv.count > 0
              text: "Item count " + lv.count + " (it actually is 0, so you should not see this text)"
            }
            ListView {
              width: 100; height: 100
              FilterModel {
                id: fm
                filter: mfilter
              }
              id: lv
              model: fm
              delegate: Text { text: "entry " + model.display }
            }
          }
        }
      }
      
      #ifndef FILTERMODEL_H
      #define FILTERMODEL_H
      
      #include <QSortFilterProxyModel>
      #include <QStringListModel>
      #include <QDebug>
      
      class FilterModel : public QSortFilterProxyModel
      {
        Q_OBJECT
        Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged)
      
        QString m_filter;
      
      public:
        explicit FilterModel(QObject *parent = 0) :
          QSortFilterProxyModel(parent)
        {
          setSourceModel(new QStringListModel(QStringList() << "AA" << "BB" << "CC", this));
        }
      
        QString filter() const { return m_filter; }
        void setFilter(const QString &filter)
        {
          qDebug() << "setFilter" << filter;
          if (m_filter != filter) {
            m_filter = filter;
            setFilterWildcard(m_filter);
            emit filterChanged();
          }
          qDebug() << "rowCount" << rowCount();
        }
      
      signals:
      
        void filterChanged();
      };
      
      #endif // FILTERMODEL_H
      

      Attachments

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

        Activity

          People

            aalpert Alan Alpert
            njeisecke Nils Jeisecke
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes