Deleting rows from QTableWidget
-
Hello guys, can someone help me?
I am trying to delete row from a QTableWidget.
So, i have:food_table = new QTableWidget(1, 2);then, it connects to a slot
connect(food_table, SIGNAL(cellChanged(int,int)), this, SLOT(checkTheCells(int,int)));slot code:
void InsertNewEatingDialog::checkTheCells(int changed_row, int changed_column) { int total_rows{food_table->rowCount()}; if(rowIsEmpty(changed_row) && total_rows > 1) food_table->removeRow(changed_row); if(tableIsFull()) food_table->insertRow(total_rows); }rowIsEmpty() and tableIsFull() are just bool member functions.
And it all works, but when the code running, when the row was deleted, i receive a message in qt developer's terminal, which says:QAbstractItemView::commitData called with an editor that does not belong to this viewand then the program continues to run normally until the next row deleting. Then that message again.
I noticed that if I replace 1 with 2 in the lineif(rowIsEmpty(changed_row) && total_rows > 1) food_table->removeRow(changed_row);it works without any messages.
Can someone explain me the cause of the error and a possible solution? -
Hi and welcome to devnet,
Any chances you have an editor opened in the row you are trying to delete ?
How are you editing your cells ?
How are you deleting your cells ? -
Hi and welcome to devnet,
Any chances you have an editor opened in the row you are trying to delete ?
How are you editing your cells ?
How are you deleting your cells ?@SGaist said in Deleting rows from QTableWidget:
welcome to devnet,
thx
@SGaist said in Deleting rows from QTableWidget:
How are you editing your cells ?
standard editing with left click (or dbl click) and entering the value, then "enter", or just click somewhere outside the editing cell
@SGaist said in Deleting rows from QTableWidget:
How are you deleting your cells ?
i'm dbl clicking the cell, then just "delete". then "enter" or click somewhere outside the cell. I understand, that this action just makes QTableWidgetItem's text empty and nothing more.but any of these actions emits signal cellChanged(int, int).
this signal is connected with my own slot checkTheCells(int, int);then, inside the signal i'm checking if row is empty. Empty means, that either:
- every cell item is empty
QTableWidgetItem* item; if(!item){...}-or
item->text().size() == 0if all cells in a row are empty, then i'm removing the whole row, because i don't need it
if(rowIsEmpty(changed_row) && total_rows > 1) food_table->removeRow(changed_row);that's all
-
The problem is that the editor is not yet closed when cellChanged()/dataChanged() is emitted and you're killing the row where the editor lives in. Use a Qt::QueuedConnection:
int main(int argc, char* argv[]) { QApplication a(argc, argv); QTableWidget tw(10, 10); QObject::connect(&tw, &QTableWidget::cellChanged, &tw, [&](int row, int) { tw.removeRow(row); }, Qt::QueuedConnection); tw.show(); return a.exec(); } -
The problem is that the editor is not yet closed when cellChanged()/dataChanged() is emitted and you're killing the row where the editor lives in. Use a Qt::QueuedConnection:
int main(int argc, char* argv[]) { QApplication a(argc, argv); QTableWidget tw(10, 10); QObject::connect(&tw, &QTableWidget::cellChanged, &tw, [&](int row, int) { tw.removeRow(row); }, Qt::QueuedConnection); tw.show(); return a.exec(); }@Christian-Ehrlicher
Yeah, that works perfectly.
But to understand, what is "the editor"? Is it an object inside QT library, or is it an external process in OS?
How can I interact with it?
Or just, where to read about it? -
@Christian-Ehrlicher
Yeah, that works perfectly.
But to understand, what is "the editor"? Is it an object inside QT library, or is it an external process in OS?
How can I interact with it?
Or just, where to read about it?@Jabik said in Deleting rows from QTableWidget:
But to understand, what is "the editor"? Is it an object inside QT library, or is it an external process in OS?
When you edit a cell, an editor is created so you can enter the new value.
See https://doc.qt.io/qt-6/model-view-programming.html#delegate-classes -
@Jabik said in Deleting rows from QTableWidget:
But to understand, what is "the editor"? Is it an object inside QT library, or is it an external process in OS?
When you edit a cell, an editor is created so you can enter the new value.
See https://doc.qt.io/qt-6/model-view-programming.html#delegate-classes@Christian-Ehrlicher
Ok, thank you, i'll read it -
J Jabik has marked this topic as solved