Deleting all rows from TableView
-
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!
-
@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().
-
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
-
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.
-
That would explain it lol. Thank you! :)
-
@aperfectcirclefan
This is all fine, but if you are "relatively new to Qt and C++" are you sure you need to be usingQAbstractTableModel
? That is fine and powerful, but it requires you to handle the model yourself and write basic code such asremoveRows()
. 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. usingQStandardItemModel
as the model for your table an easier introduction to models + tables, perhaps while you learn till you are ready to move to your ownQAbstractTableModel
. The nice thing in Qt is thatQTableView
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. -
@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!
-
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.
-
@SimonSchroeder Interesting I'm going to try that then since it seems more "directed" if that makes sense!