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. Release memory from QAbstractItemModel
Forum Updated to NodeBB v4.3 + New Features

Release memory from QAbstractItemModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 5 Posters 674 Views 2 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.
  • M Offline
    M Offline
    manny_lp
    wrote on last edited by
    #1

    Hi all,

    I am using QAbstractItemModel to maintain a list in qt application. If I use
    foreach (QObject *obj, _items) {
    obj->deleteLater();
    }
    _items.clear();

    whether it will release the memory ? Because in case std::vector memory remains attached to the vector after vector clear. Is it same with QAbstractItemModel?

    eyllanescE 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What is _items ?
      How did you create it ?

      You know that even if you release memory it might not be immediately claimed by the OS ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M manny_lp

        Hi all,

        I am using QAbstractItemModel to maintain a list in qt application. If I use
        foreach (QObject *obj, _items) {
        obj->deleteLater();
        }
        _items.clear();

        whether it will release the memory ? Because in case std::vector memory remains attached to the vector after vector clear. Is it same with QAbstractItemModel?

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by eyllanesc
        #3

        @manny_lp Use qDeleteAll():

        beginResetModel();
        qDeleteAll(_items.begin(), _items.end());
        _items.clear();
        endResetModel();
        

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        fcarneyF 1 Reply Last reply
        1
        • eyllanescE eyllanesc

          @manny_lp Use qDeleteAll():

          beginResetModel();
          qDeleteAll(_items.begin(), _items.end());
          _items.clear();
          endResetModel();
          
          fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          @eyllanesc said in Release memory from QAbstractItemModel:

          qDeleteAll(_items.begin(), _items.end());

          This might work too:

          qDeleteAll(_items);
          

          @manny_lp
          Avoid using underscores:
          https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            manny_lp
            wrote on last edited by
            #5

            @SGaist _items is list of qobjects. i.e QList<QObject *> _items;

            #include "listmodel.h"
            #include "qdebug.h"
            using namespace models;
            
            ListModel::ListModel(QObject *parent): QAbstractListModel(parent)
            {
            
            }
            
            int ListModel::rowCount(const QModelIndex &parent) const
            {
                return parent.isValid() ? 0 : _items.size();
            }
            
            QVariant ListModel::data(const QModelIndex &index, int role) const
            {
                if ((index.row() >= 0 || index.row() < _items.count()) &&
                    role == ItemRole) {
                    if(index.isValid())
                        return QVariant::fromValue(_items[index.row()]);
                }
                return QVariant();
            }
            // cppcheck-suppress unusedFunction
            QHash<int, QByteArray> ListModel::roleNames() const
            {
                QHash<int, QByteArray> roles;
                roles[ItemRole] = "item";
                return roles;
            }
            
            void ListModel::clearItems()
            {
                foreach (QObject *obj, _items) {
                    obj->deleteLater();
                }
                _items.clear();
            }
            #ifndef LISTMODEL_H
            #define LISTMODEL_H
            
            #include <QAbstractListModel>
            
            namespace models
            {
            
            /**
             * @brief The ListModel class
             * Inherited from QAbstractListModel
             * Can be used by all listmodel implementations
             */
            class ListModel : public QAbstractListModel
            {
            public:
                // User defined roles
                enum Roles
                {
                    ItemRole = Qt::UserRole + 1
                };
            
                explicit ListModel(QObject *parent = nullptr);
            
                /**
                 * @brief rowCount - Returns the number of rows under the given parent.
                 * @param parent - QModelIndex
                 * @return - int
                 */
                virtual int rowCount(const QModelIndex &parent) const;
                /**
                 * @brief data - Returns the data stored under the given role for the item referred to by the index.
                 * @param index - index to refer. Type: QModelIndex
                 * @param role - Role to access. Type: int/enum
                 * @return - QVariant
                 */
                virtual QVariant data(const QModelIndex &index, int role) const;
                /**
                 * @brief roleNames - Returns the model's role names.
                 * @return - QHash<int, QByteArray>
                 */
                virtual QHash<int, QByteArray> roleNames() const;
            
                /**
                 * @brief items - return a ref to list of items
                 * @return - QList<QObject *>
                 */
                const QList<QObject *> &items() const
                {
                    return _items;
                }
                /**
                 * @brief size - returns the size of the list
                 * @return - int
                 */
                int size() const
                {
                    return _items.size();
                }
            
            protected:
                // Container to hold the data
                QList<QObject *> _items;
                QList<QObject *> _emptyListToSwap;
            
                /**
                 * @brief clearItems - Clear all the items in the list
                 */
                void clearItems();
            };
            
            }
            
            #endif // LISTMODEL_H
            
            

            qDeleteAll also does not help.

            JonBJ 1 Reply Last reply
            0
            • M manny_lp

              @SGaist _items is list of qobjects. i.e QList<QObject *> _items;

              #include "listmodel.h"
              #include "qdebug.h"
              using namespace models;
              
              ListModel::ListModel(QObject *parent): QAbstractListModel(parent)
              {
              
              }
              
              int ListModel::rowCount(const QModelIndex &parent) const
              {
                  return parent.isValid() ? 0 : _items.size();
              }
              
              QVariant ListModel::data(const QModelIndex &index, int role) const
              {
                  if ((index.row() >= 0 || index.row() < _items.count()) &&
                      role == ItemRole) {
                      if(index.isValid())
                          return QVariant::fromValue(_items[index.row()]);
                  }
                  return QVariant();
              }
              // cppcheck-suppress unusedFunction
              QHash<int, QByteArray> ListModel::roleNames() const
              {
                  QHash<int, QByteArray> roles;
                  roles[ItemRole] = "item";
                  return roles;
              }
              
              void ListModel::clearItems()
              {
                  foreach (QObject *obj, _items) {
                      obj->deleteLater();
                  }
                  _items.clear();
              }
              #ifndef LISTMODEL_H
              #define LISTMODEL_H
              
              #include <QAbstractListModel>
              
              namespace models
              {
              
              /**
               * @brief The ListModel class
               * Inherited from QAbstractListModel
               * Can be used by all listmodel implementations
               */
              class ListModel : public QAbstractListModel
              {
              public:
                  // User defined roles
                  enum Roles
                  {
                      ItemRole = Qt::UserRole + 1
                  };
              
                  explicit ListModel(QObject *parent = nullptr);
              
                  /**
                   * @brief rowCount - Returns the number of rows under the given parent.
                   * @param parent - QModelIndex
                   * @return - int
                   */
                  virtual int rowCount(const QModelIndex &parent) const;
                  /**
                   * @brief data - Returns the data stored under the given role for the item referred to by the index.
                   * @param index - index to refer. Type: QModelIndex
                   * @param role - Role to access. Type: int/enum
                   * @return - QVariant
                   */
                  virtual QVariant data(const QModelIndex &index, int role) const;
                  /**
                   * @brief roleNames - Returns the model's role names.
                   * @return - QHash<int, QByteArray>
                   */
                  virtual QHash<int, QByteArray> roleNames() const;
              
                  /**
                   * @brief items - return a ref to list of items
                   * @return - QList<QObject *>
                   */
                  const QList<QObject *> &items() const
                  {
                      return _items;
                  }
                  /**
                   * @brief size - returns the size of the list
                   * @return - int
                   */
                  int size() const
                  {
                      return _items.size();
                  }
              
              protected:
                  // Container to hold the data
                  QList<QObject *> _items;
                  QList<QObject *> _emptyListToSwap;
              
                  /**
                   * @brief clearItems - Clear all the items in the list
                   */
                  void clearItems();
              };
              
              }
              
              #endif // LISTMODEL_H
              
              

              qDeleteAll also does not help.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @manny_lp said in Release memory from QAbstractItemModel:

              qDeleteAll also does not help.

              ? What exactly is your issue? (Apart from the fact that you don't seem to actually call ListModel::clearItems())?

              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