Removing rows from TableModel
-
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 ?
-
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 ?
-
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 ?
@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 buttable->removeRows(0, table->rowCount(QModelIndex()));
doesn't work. So yes, can you clarify me this example ?
-
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? ...
- What is your
table->editStrategy()
? Docs state that onlyOnManualSubmit
allows mutliple-row deletion. And then you'll need asubmitAll()
. - Check the return result of your
table->removeRows()
, andlastError()
if that'sfalse
. - I think
table->clear()
is a quick way of deleting all rows, though that may not affect what you are wanting to learn about.
- What is your
-
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 ?
-
@JNBarchan
My
TableModel
comes fromQAbstractTableModel
.
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 -
@JNBarchan
My
TableModel
comes fromQAbstractTableModel
.
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@theo_ld
Yeah, I had assumedQSqlTableModel
....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 implementedremoveRows()
is does nothing? You sure the example which did work did not useremoveRow()
(note the spelling)? -
@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 -
@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... ;-)