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 remove rows from QStandardItemModel?
Forum Update on Monday, May 27th 2025

How to remove rows from QStandardItemModel?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 2.1k Views
  • 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.
  • D Offline
    D Offline
    deleted385
    wrote on last edited by deleted385
    #1

    In the QTableView of the QCompleter I add all object names with tag when a database is attached. When a new database is attached I want to remove the previous database object names before adding news BUT the current approach doesn't work:

    x1.gif
    So when I attached reverso.db, I've added all those object names (Table, View, Index, Trigger and Column) and when I attached quran.db those old names from reverso.db remains there. Here's the approach I've tried:

    void QueryView::onDbChanged(QList<QString>& list)
    {
        QVector<QString> itemsToRemove = {"Table", "View", "Index", "Trigger", "Column"};
        QList<QPair<int, QModelIndex>> tobeRemoved;
        auto model = editor->getCompleterModel();
        qDebug() << "before " << model->rowCount();
        for (int i = 0; i < model->rowCount(); i++) {
            auto index = model->index(i, 1);
            if(itemsToRemove.contains(model->data(index).toString()))
                tobeRemoved.append(QPair<int, QModelIndex>(i, index));
        }
        foreach(auto row, tobeRemoved) model->removeRow(row.first, row.second);
        qDebug() << "after " << model->rowCount();
       ...
     }
    

    How to remove those old object names from the QStandardItemModel, model?

    JonBJ 1 Reply Last reply
    0
    • D deleted385

      @JonB, I've replaced the foreach with:

      for (int i = tobeRemoved.count() - 1; i >= 0; i--)
          model->removeRow(tobeRemoved[i].first, tobeRemoved[i].second);
      

      but it doesn't remove. From the qDebug output I get:

      before  184
      after  184
      

      so it actually doesn't remove any row.

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

      @Emon-Haque
      First of all, you will need that reverse-ordering-removal anyway, so keep that principle.

      So far as I can see, you have a QTableView, not a QTreeView. So I assume your model is "flat", not "hierarchical" like a tree? In that case you save up model->index(i, 1), whatever that is, and pass it via model->removeRow(row.first, row.second) as row.second. But bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent = QModelIndex())

      Removes the given row from the child items of the parent specified

      What are you doing passing any parent? Do you just want to omit that parameter?

      D 2 Replies Last reply
      3
      • D deleted385

        In the QTableView of the QCompleter I add all object names with tag when a database is attached. When a new database is attached I want to remove the previous database object names before adding news BUT the current approach doesn't work:

        x1.gif
        So when I attached reverso.db, I've added all those object names (Table, View, Index, Trigger and Column) and when I attached quran.db those old names from reverso.db remains there. Here's the approach I've tried:

        void QueryView::onDbChanged(QList<QString>& list)
        {
            QVector<QString> itemsToRemove = {"Table", "View", "Index", "Trigger", "Column"};
            QList<QPair<int, QModelIndex>> tobeRemoved;
            auto model = editor->getCompleterModel();
            qDebug() << "before " << model->rowCount();
            for (int i = 0; i < model->rowCount(); i++) {
                auto index = model->index(i, 1);
                if(itemsToRemove.contains(model->data(index).toString()))
                    tobeRemoved.append(QPair<int, QModelIndex>(i, index));
            }
            foreach(auto row, tobeRemoved) model->removeRow(row.first, row.second);
            qDebug() << "after " << model->rowCount();
           ...
         }
        

        How to remove those old object names from the QStandardItemModel, model?

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

        @Emon-Haque said in How to remove rows from QStandardItemModel?:

        foreach(auto row, tobeRemoved) model->removeRow(row.first, row.second);

        Your row.first() holds the original row numbers from the earlier for (int i ... loop, like 0, 1, 2, .... But as soon as you execute the first model->removeRow(row.first /* this is 0 */, row.second); the next row, which was originally 1, is now 0, so you aren't removing what you think you are (you are leaving some items). Print out the bool return result from model->removeRow(row.first, row.second);, you should see that the higher numbers return false.

        D 1 Reply Last reply
        1
        • JonBJ JonB

          @Emon-Haque said in How to remove rows from QStandardItemModel?:

          foreach(auto row, tobeRemoved) model->removeRow(row.first, row.second);

          Your row.first() holds the original row numbers from the earlier for (int i ... loop, like 0, 1, 2, .... But as soon as you execute the first model->removeRow(row.first /* this is 0 */, row.second); the next row, which was originally 1, is now 0, so you aren't removing what you think you are (you are leaving some items). Print out the bool return result from model->removeRow(row.first, row.second);, you should see that the higher numbers return false.

          D Offline
          D Offline
          deleted385
          wrote on last edited by
          #3

          @JonB, right. It'd be nice if I could pass the model->data() (QVariant) in a remove function.

          JonBJ 1 Reply Last reply
          0
          • D deleted385

            @JonB, right. It'd be nice if I could pass the model->data() (QVariant) in a remove function.

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

            @Emon-Haque
            Well you can't! The simplest from here is get rid of your foreach(auto row, ..., which visits your rows in ascending row number and so gets the numbering wrong during removals, to for (int i = tobeRemoved.count() - 1; i >= 0; i--), which visits the row numbers in descending order, and should then work correctly? [Or populate itemsToRemove in this reverse order if you prefer.]

            D 1 Reply Last reply
            1
            • JonBJ JonB

              @Emon-Haque
              Well you can't! The simplest from here is get rid of your foreach(auto row, ..., which visits your rows in ascending row number and so gets the numbering wrong during removals, to for (int i = tobeRemoved.count() - 1; i >= 0; i--), which visits the row numbers in descending order, and should then work correctly? [Or populate itemsToRemove in this reverse order if you prefer.]

              D Offline
              D Offline
              deleted385
              wrote on last edited by
              #5

              @JonB, I've replaced the foreach with:

              for (int i = tobeRemoved.count() - 1; i >= 0; i--)
                  model->removeRow(tobeRemoved[i].first, tobeRemoved[i].second);
              

              but it doesn't remove. From the qDebug output I get:

              before  184
              after  184
              

              so it actually doesn't remove any row.

              JonBJ 1 Reply Last reply
              0
              • D deleted385

                @JonB, I've replaced the foreach with:

                for (int i = tobeRemoved.count() - 1; i >= 0; i--)
                    model->removeRow(tobeRemoved[i].first, tobeRemoved[i].second);
                

                but it doesn't remove. From the qDebug output I get:

                before  184
                after  184
                

                so it actually doesn't remove any row.

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

                @Emon-Haque
                First of all, you will need that reverse-ordering-removal anyway, so keep that principle.

                So far as I can see, you have a QTableView, not a QTreeView. So I assume your model is "flat", not "hierarchical" like a tree? In that case you save up model->index(i, 1), whatever that is, and pass it via model->removeRow(row.first, row.second) as row.second. But bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent = QModelIndex())

                Removes the given row from the child items of the parent specified

                What are you doing passing any parent? Do you just want to omit that parameter?

                D 2 Replies Last reply
                3
                • JonBJ JonB

                  @Emon-Haque
                  First of all, you will need that reverse-ordering-removal anyway, so keep that principle.

                  So far as I can see, you have a QTableView, not a QTreeView. So I assume your model is "flat", not "hierarchical" like a tree? In that case you save up model->index(i, 1), whatever that is, and pass it via model->removeRow(row.first, row.second) as row.second. But bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent = QModelIndex())

                  Removes the given row from the child items of the parent specified

                  What are you doing passing any parent? Do you just want to omit that parameter?

                  D Offline
                  D Offline
                  deleted385
                  wrote on last edited by
                  #7

                  @JonB, in the second for If I do model->removeRow(tobeRemoved[i].second.row()) it works.
                  Thanks.

                  1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Emon-Haque
                    First of all, you will need that reverse-ordering-removal anyway, so keep that principle.

                    So far as I can see, you have a QTableView, not a QTreeView. So I assume your model is "flat", not "hierarchical" like a tree? In that case you save up model->index(i, 1), whatever that is, and pass it via model->removeRow(row.first, row.second) as row.second. But bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent = QModelIndex())

                    Removes the given row from the child items of the parent specified

                    What are you doing passing any parent? Do you just want to omit that parameter?

                    D Offline
                    D Offline
                    deleted385
                    wrote on last edited by
                    #8

                    @JonB, one more thing, hope you wouldn't mind. Do I have to call delete on QStandardItem in each column of those rows I'm removing or the Qt framework takes care of that automatically?

                    JonBJ 1 Reply Last reply
                    0
                    • D deleted385

                      @JonB, one more thing, hope you wouldn't mind. Do I have to call delete on QStandardItem in each column of those rows I'm removing or the Qt framework takes care of that automatically?

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

                      @Emon-Haque
                      No, the model took ownership of the items when you added them and is responsible for deleting them when you remove them from the model (via removeRow(), but not if you used takeRow(), see https://www.qtcentre.org/threads/63505-removeRow-or-takeRow-in-QStandardItemModel).

                      1 Reply Last reply
                      3

                      • Login

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