Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML List View is not updating when the model is updated
Forum Updated to NodeBB v4.3 + New Features

QML List View is not updating when the model is updated

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 375 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yegender
    wrote on last edited by
    #1

    Hi everyone,

    When, when the data is added or removed, qml model doesn't update. But when I change the existing data it updates.

    Function that add data:

            beginInsertRows(QModelIndex(), rowCount(), rowCount());
            ApplicationItem *item = new ApplicationItem;
            item->id = id;
            item->iconName = info.value("iconName").toString();
            item->visibleName = info.value("visibleName").toString();
            item->isActive = info.value("active").toBool();
            item->wids.append(wid);
    
            if (!desktopPath.isEmpty()) {
                QMap<QString, QString> desktopInfo = Utils::instance()->readInfoFromDesktop(desktopPath);
                item->iconName = desktopInfo.value("Icon");
                item->visibleName = desktopInfo.value("Name");
                item->exec = desktopInfo.value("Exec");
                item->desktopPath = desktopPath;
            }
    
            m_Items << item;
            endInsertRows();
    
            emit itemAdded();
            emit countChanged();
    

    and the remove data function:

        ApplicationItem *item = findItembyWId(wid);
                
                    if (!item)
                        return;
                
                    item->wids.removeOne(wid);
                
                    if (item->currentActive >= item->wids.size())
                        item->currentActive = 0;
                
                    handleDataChangedFromItem(item);
                
                    if (item->wids.isEmpty()) {
                        if (!item->isPinned) {
                            int index = indexOf(item->id);
                
                            if (index == -1)
                                return;
                
                            beginRemoveRows(QModelIndex(), index, index);
                            m_Items.removeAll(item);
                            endRemoveRows();
                
                            emit itemRemoved();
                            emit countChanged();
                        }
                    }
    

    There isn't special in QML. It's implemented in standard way.

    Thank you

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yegender
      wrote on last edited by
      #2

      I found out that row count isn't updating. It's updating in every part of the class except the rowCount() function. When I called list size, when a item is appending, it updated, but rowCount() doesn't. Here is the function:

      int AppModel::rowCount(const QModelIndex &parent = QModelIndex()) const
      {
          Q_UNUSED(parent)
          return m_Items.size();
      }```
      piervalliP 1 Reply Last reply
      0
      • Y yegender

        I found out that row count isn't updating. It's updating in every part of the class except the rowCount() function. When I called list size, when a item is appending, it updated, but rowCount() doesn't. Here is the function:

        int AppModel::rowCount(const QModelIndex &parent = QModelIndex()) const
        {
            Q_UNUSED(parent)
            return m_Items.size();
        }```
        piervalliP Offline
        piervalliP Offline
        piervalli
        wrote on last edited by
        #3

        @yegender
        In the beginRemoveRows you need specify the range of rows to remove. The function removeAll clear a one or more items?

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yegender
          wrote on last edited by
          #4

          @piervalli It's assumed to clear one. As the reference code I used a tutorial for this app removes one Item. I found that rowCount() always return same value in qml. But it updates in c++. and i used qDebug() << m_Items.size() in this function. It also gives me same number. I am confused. what's going on. If you want to see full code it's :

          header file: https://github.com/yegender124001/avdan-os-dock/blob/master/appmodel.h

          definations: https://github.com/yegender124001/avdan-os-dock/blob/master/appmodel.cpp

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved