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. Removing rows from TableModel
Forum Updated to NodeBB v4.3 + New Features

Removing rows from TableModel

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 4.3k 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.
  • T Offline
    T Offline
    theo_ld
    wrote on last edited by
    #1

    I'm currently learning Qt and I've found a Qt Address Book Example.

    I would like to change some features but I've been struggling with the TableModel class.
    This method used to work with a user interaction, the user clicks on a contact and then clicks the "Delete contact" button to delete the contact.

    I'd like to implement instead a method that simply deletes all the entries in my TableModel table.

    IpWidget.h

    #ifndef IPWIDGET_H
    #define IPWIDGET_H
    
    #include "TableModel.h"
    
    #include <QItemSelection>
    #include <QTabWidget>
    
    class QSortFilterProxyModel;
    class QItemSelectionModel;
    
    class IpWidget : public QTabWidget
    {
        Q_OBJECT
    
    public:
        IpWidget(QWidget *parent = 0);
        void addEntry(QString name, QString address);
        void removeAllEntries();
    
    signals:
        void selectionChanged (const QItemSelection &selected);
    
    private:
        void setupTabs();
    
        TableModel *table;
        QSortFilterProxyModel *proxyModel;
    };
    
    #endif // IPWIDGET_H
    
    

    IpWidget.cpp

    void IpWidget::addEntry(QString name, QString ip)
    {
        QList<QPair<QString, QString>> list = table->getList();
        QPair<QString, QString> pair(name, ip);
    
        table->insertRows(0, 1, QModelIndex());
    
        QModelIndex index = table->index(0, 0, QModelIndex());
        table->setData(index, name, Qt::EditRole);
    
        index = table->index(0, 1, QModelIndex());
        table->setData(index, ip, Qt::EditRole);
    }
    
    void IpWidget::removeEntry()
    {
        QTableView *temp = static_cast<QTableView*>(currentWidget());
        QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
        QItemSelectionModel *selectionModel = temp->selectionModel();
    
        QModelIndexList indexes = selectionModel->selectedRows();
    
        foreach (QModelIndex index, indexes) {
            int row = proxy->mapToSource(index).row();
            table->removeRows(row, 1, QModelIndex());
        }
    }
    
    void IpWidget::removeAllEntries()
    {
        table->removeRows(0, table->rowCount(QModelIndex()), QModelIndex());
    }
    
    

    I wrote this, but I'm not sure it's correct, could you guys clarify me this class ?

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

      Hi and welcome to devnet,

      Just to be sure I understand your question correctly, are you asking us to review your code and explain it to you ?

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

      T 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        Just to be sure I understand your question correctly, are you asking us to review your code and explain it to you ?

        T Offline
        T Offline
        theo_ld
        wrote on last edited by
        #3

        @SGaist I said this code was coming from Qt Address Book Example. I've tried it, but I'd like to change some features.
        By the way, i'm not sure to understand how the TableModel works. I tried to clear it but

        table->removeRows(0, table->rowCount(QModelIndex()));
        

        doesn't work. So yes, can you clarify me this example ?

        1 Reply Last reply
        0
        • JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @theo_ld

          table->removeRows(0, table->rowCount(QModelIndex()));
          

          doesn't work. So yes, can you clarify me this example ?

          What does "doesn't work" mean? Compile error? Runtime error? Not the behaviour you expected? ...

          1. What is your table->editStrategy()? Docs state that only OnManualSubmit allows mutliple-row deletion. And then you'll need a submitAll().
          2. Check the return result of your table->removeRows(), and lastError() if that's false.
          3. I think table->clear() is a quick way of deleting all rows, though that may not affect what you are wanting to learn about.
          1 Reply Last reply
          0
          • T Offline
            T Offline
            theo_ld
            wrote on last edited by theo_ld
            #5

            I run through a Runtime Error ! I'm trying to fix it with your advice ! I believe the

            table->clear()
            

            method doesn't exist in TableMethod btw ;) Can you use

            table->removeRows()
            

            it if the table is empty ?

            JonBJ 1 Reply Last reply
            0
            • T theo_ld

              I run through a Runtime Error ! I'm trying to fix it with your advice ! I believe the

              table->clear()
              

              method doesn't exist in TableMethod btw ;) Can you use

              table->removeRows()
              

              it if the table is empty ?

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

              @theo_ld

              So tell us the runtime error! And/or check the return result + lastError()!

              clear method: what is your TableModel derived from?

              1 Reply Last reply
              0
              • T Offline
                T Offline
                theo_ld
                wrote on last edited by theo_ld
                #7

                @JNBarchan

                My TableModel comes from QAbstractTableModel.
                lastError() works only for SQL Databases right ? Which object is it stored in ?
                Can't check the return of the function because I get a runtime error.

                Here is the console output

                Debugging starts
                ASSERT: "last >= first" in file itemmodels\qabstractitemmodel.cpp, line 2743
                Debugging has finished

                JonBJ 1 Reply Last reply
                0
                • T theo_ld

                  @JNBarchan

                  My TableModel comes from QAbstractTableModel.
                  lastError() works only for SQL Databases right ? Which object is it stored in ?
                  Can't check the return of the function because I get a runtime error.

                  Here is the console output

                  Debugging starts
                  ASSERT: "last >= first" in file itemmodels\qabstractitemmodel.cpp, line 2743
                  Debugging has finished

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

                  @theo_ld
                  Yeah, I had assumed QSqlTableModel....

                  ASSERT: "last >= first" in file itemmodels\qabstractitemmodel.cpp, line 2743

                  I would guess this is telling you that your start row number + number of rows >= last row in table.

                  So: look at value of table->rowCount(QModelIndex()). Is it 0? In which case, if there are 0 rows, no, you cannot delete any rows from an empty table, even if you specify a count of 0 rows to delete. At a guess!

                  P.S.
                  Also, QAbstractTableModel requires you to subclass (TableModel), and unless that has implemented removeRows() is does nothing? You sure the example which did work did not use removeRow() (note the spelling)?

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    theo_ld
                    wrote on last edited by theo_ld
                    #9

                    @JNBarchan You might be right, so I added a condition and it doesn't runtime anymore.

                    Although, my user isn't visible at all in the graphical interface -_-.
                    Trying to fix this problem now :p

                    JonBJ 1 Reply Last reply
                    0
                    • T theo_ld

                      @JNBarchan You might be right, so I added a condition and it doesn't runtime anymore.

                      Although, my user isn't visible at all in the graphical interface -_-.
                      Trying to fix this problem now :p

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

                      @theo_ld said in Removing rows from TableModel:

                      @JNBarchan You might be right, so I added a condition and it doesn't runtime anymore.

                      In that case, I'd suggest I was right... ;-)

                      T 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @theo_ld said in Removing rows from TableModel:

                        @JNBarchan You might be right, so I added a condition and it doesn't runtime anymore.

                        In that case, I'd suggest I was right... ;-)

                        T Offline
                        T Offline
                        theo_ld
                        wrote on last edited by
                        #11
                        This post is deleted!
                        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