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

Deleting all rows from TableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 737 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.
  • A Offline
    A Offline
    aperfectcirclefan
    wrote on last edited by
    #1

    Hey all!

    I'm relatively new to Qt and C++, and am having a heck of a time trying to figure out how to correctly clear out my table data upon a button press.

    Through my internet searching I found a few functions from Qt that are supposed to do the job, but I can't figure out what i'm missing that is causing it to throw a error. Here is where I call the delete function:

    mainwindow.cpp

    void MainWindow::on_searchButton_clicked()
    {
        quint8 contactIndex = ui->objectType->currentIndex();
        contactData.clearAll();
        MainWindow::TestConnection(contactIndex);
    }
    
    

    and here is the actual function itself

    contactData.cpp

    void ContactData::clearAll(){
        this->beginRemoveRows(QModelIndex(), 0, m_userData.size());
        this->removeRows(0, m_userData.size());
        this->endRemoveRows();
    }
    

    When I attempt to run the program I receive this error:

    ASSERT:  "last < rowCount(parent)" in file
    

    and then references a what I presume is internal Qt file:

    ASSERT: "last < rowCount(parent)" in file C:\Users\qt\work\qt\qtbase\src\corelib\itemmodels\qabstractitemmodel.cpp, line 2931
    

    I presume i'm missing something incredibly obvious, so if anyone could provide a hint or a solution it would be greatly appreciated! :) If i'm doing it entirely wrong also please don't be afraid to tell me!

    Thank you!

    Christian EhrlicherC 1 Reply Last reply
    0
    • A aperfectcirclefan

      Hey all!

      I'm relatively new to Qt and C++, and am having a heck of a time trying to figure out how to correctly clear out my table data upon a button press.

      Through my internet searching I found a few functions from Qt that are supposed to do the job, but I can't figure out what i'm missing that is causing it to throw a error. Here is where I call the delete function:

      mainwindow.cpp

      void MainWindow::on_searchButton_clicked()
      {
          quint8 contactIndex = ui->objectType->currentIndex();
          contactData.clearAll();
          MainWindow::TestConnection(contactIndex);
      }
      
      

      and here is the actual function itself

      contactData.cpp

      void ContactData::clearAll(){
          this->beginRemoveRows(QModelIndex(), 0, m_userData.size());
          this->removeRows(0, m_userData.size());
          this->endRemoveRows();
      }
      

      When I attempt to run the program I receive this error:

      ASSERT:  "last < rowCount(parent)" in file
      

      and then references a what I presume is internal Qt file:

      ASSERT: "last < rowCount(parent)" in file C:\Users\qt\work\qt\qtbase\src\corelib\itemmodels\qabstractitemmodel.cpp, line 2931
      

      I presume i'm missing something incredibly obvious, so if anyone could provide a hint or a solution it would be greatly appreciated! :) If i'm doing it entirely wrong also please don't be afraid to tell me!

      Thank you!

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @aperfectcirclefan said in Deleting all rows from TableView:

      this->beginRemoveRows(QModelIndex(), 0, m_userData.size());
      this->removeRows(0, m_userData.size());
      this->endRemoveRows();
      

      What is ContactData? QAbstractTableModel? What dos removeRows() do? Normally removeRows should call begin/endRemoveRows().

      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
      0
      • A Offline
        A Offline
        aperfectcirclefan
        wrote on last edited by
        #3

        Thanks for the reply and my apologies for not surfacing that information!

        ContactData inherits from QAbstractTableModel yeah;

        To be honest, i'm not sure what removeRows() does; i think I found it here and tried it out lol https://forum.qt.io/topic/139600/removing-rows-from-a-table-model

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

          So you did not reimplement removeRows()? Then it does nothing as written in the documentation. You either have to implement it or do not call it and clear your data by yourself.

          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
          0
          • A Offline
            A Offline
            aperfectcirclefan
            wrote on last edited by
            #5

            That would explain it lol. Thank you! :)

            JonBJ 1 Reply Last reply
            0
            • A aperfectcirclefan

              That would explain it lol. Thank you! :)

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

              @aperfectcirclefan
              This is all fine, but if you are "relatively new to Qt and C++" are you sure you need to be using QAbstractTableModel? That is fine and powerful, but it requires you to handle the model yourself and write basic code such as removeRows(). Are you at least aware that Qt supplies "pre-built" generic models for you to use with QStandardItemModel for your own data and QSqlQueryModel if you have your data in a SQL database? You might find e.g. using QStandardItemModel as the model for your table an easier introduction to models + tables, perhaps while you learn till you are ready to move to your own QAbstractTableModel. The nice thing in Qt is that QTableView works against whichever kind of model you use, and you should be able to change between individual models over time while keeping the UI the same.

              1 Reply Last reply
              1
              • A Offline
                A Offline
                aperfectcirclefan
                wrote on last edited by
                #7

                @JonB I was not aware of that stuff! That probably makes more sense for my current skill level, so I'll look into converting to that. Thank you!

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SimonSchroeder
                  wrote on last edited by
                  #8

                  I agree with @JonB that QStandardItemModel is a lot easier. However, I rarely need a model in my applications. Most of the time I'm quite content with the QTableWidget instead of the QTableView. This is even more beginner friendly, IMHO. QTableWidget inherits from QTableView, but has its own model. You can just add QTableWidgetItems.

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    aperfectcirclefan
                    wrote on last edited by
                    #9

                    @SimonSchroeder Interesting I'm going to try that then since it seems more "directed" if that makes sense!

                    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