Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to reset a custom model?
Forum Updated to NodeBB v4.3 + New Features

How to reset a custom model?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 8.1k 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
    Mr Gisa
    wrote on last edited by Mr Gisa
    #1

    I have a custom model that I'm studying and I was wondering, how can I reset the model and empty the items on the model and on the table view?

    #ifndef RESULTSMODEL_HPP
    #define RESULTSMODEL_HPP
    
    #include <QAbstractTableModel>
    
    class Result;
    
    class ResultsModel : public QAbstractTableModel
    {
        Q_OBJECT
    
    public:
        explicit ResultsModel(QObject *parent = nullptr);
    
        int rowCount(const QModelIndex &parent) const override;
        int columnCount(const QModelIndex &parent) const override;
        QVariant data(const QModelIndex &index, int role) const override;
        bool setData(const QModelIndex &index, const QVariant &value, int role) override;
        QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
        Qt::ItemFlags flags(const QModelIndex &index) const override;
    
        void addResult(Result *result);
    
    private:
        QList<Result *> mResults;
    };
    
    #endif // RESULTSMODEL_HPP
    

    The model holds the parenting of the Result * but I wanted that when resetting to delete the pointers as well.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      And what prevents you from deleting the pointer by yourself?
      Make sure you call the functions beginResetModel/endRestModel as described in the documentation: http://doc.qt.io/qt-5/qabstractitemmodel.html#beginResetModel

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Mr Gisa
        wrote on last edited by Mr Gisa
        #3

        That is the thing, I never did the beginResetModel and I read the docs but I couldn't find an example of the usage, where to call that and so on.

        I tried like this but it's crashing:

        void ResultsModel::reset()
        {
            beginResetModel();
            qDeleteAll(mResults);
            endResetModel();
        }
        
        JKSHJ 1 Reply Last reply
        0
        • M Mr Gisa

          That is the thing, I never did the beginResetModel and I read the docs but I couldn't find an example of the usage, where to call that and so on.

          I tried like this but it's crashing:

          void ResultsModel::reset()
          {
              beginResetModel();
              qDeleteAll(mResults);
              endResetModel();
          }
          
          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by JKSH
          #4

          @Mr-Gisa said in How to reset a custom model?:

          I tried like this but it's crashing:

          void ResultsModel::reset()
          {
              beginResetModel();
              qDeleteAll(mResults);
              endResetModel();
          }
          

          After you call qDeleteAll(), you now have a QList full of dangling pointers.

          See http://doc.qt.io/qt-5/qtalgorithms.html#qDeleteAll : "Notice that qDeleteAll() doesn't remove the items from the container; it merely calls delete on them. In the example above, we call clear() on the container to remove the items."

          @Mr-Gisa said in How to reset a custom model?:

          how can I reset the model and empty the items on the model and on the table view?

          You're almost there. You just need to remove the dangling pointers. The basic steps for all models are:

          1. Call beginResetModel() to tell the views to stop reading your model
          2. Use your custom code to reset the internal data
          3. Call endResetModel() to tell the views that the model has been reset. The views will now re-read the model to discover the updated data.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          1
          • M Offline
            M Offline
            Mr Gisa
            wrote on last edited by Mr Gisa
            #5

            @JKSH

            Use your custom code to reset the internal data

            I tried to use mResults.clear() it worked but now I get a lot of pointers leaking.

            I can't call qDeleteAll(mResults) cause the list is empty.

                beginResetModel();
                mResults.clear();
                endResetModel();
            
                qDeleteAll(mResults); // won't work.
            
            JKSHJ 1 Reply Last reply
            0
            • M Mr Gisa

              @JKSH

              Use your custom code to reset the internal data

              I tried to use mResults.clear() it worked but now I get a lot of pointers leaking.

              I can't call qDeleteAll(mResults) cause the list is empty.

                  beginResetModel();
                  mResults.clear();
                  endResetModel();
              
                  qDeleteAll(mResults); // won't work.
              
              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              @Mr-Gisa said in How to reset a custom model?:

              I tried to use mResults.clear() it worked but now I get a lot of pointers leaking.

              I can't call qDeleteAll(mResults) cause the list is empty.

              See the example code in the documentation. I provided the link in the previous post.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              2
              • M Offline
                M Offline
                Mr Gisa
                wrote on last edited by
                #7

                Oh gosh, sorry, I though that you mentioned only that part of the documentation and wasn't anything there other than that, my bad
                It solved, thank you very much.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gabizzz
                  wrote on last edited by
                  #8

                  import QtQuick 2.11
                  import QtQuick.Window 2.11

                  Window {

                  id: window
                  visible: true
                  width: 300
                  height: 480
                  color: "#050122"
                  title: qsTr("sortea numeros")   
                  
                  Grid{
                      spacing: 1
                      rows: 3
                      columns: 2
                      anchors.fill: parent
                  
                      Repeater {
                          id:modelo
                              model: 6
                              delegate: Rectangle {
                                    width: window.width/6;
                                    height: window.height/6;
                                    color: "darkblue";
                                          Text {
                                              anchors.centerIn: parent
                                              font.pixelSize: 20
                                              text: sortea()
                                          }
                                  }
                      }
                  }
                  
                  
                  MouseArea {
                      id: mouseArea
                      x: 220
                      y: 387
                      anchors.fill: parent
                      onClicked: {
                          modelo.model=0;
                          modelo.model=6;
                      }
                  }
                  
                  function sortea()
                  {
                      var random =Math.floor(Math.random() * 6) + 1;
                      return random;
                  }
                  

                  }

                  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