QTableManager Removing All Rows Crashes Application with Bad Access
-
I have a QTableWidget that I drew in QT Creator, as well as added rows there. I tried using the following techniques...
http://stackoverflow.com/questions/15848086/how-to-delete-all-rows-from-qtablewidget
...but the application crashes with a bad access after it removes all the rows.
However, if I leave one row behind, it doesn't crash. For instance:
ui->myTable->model()->removeRows(0,ui->myTable->rowCount()-1); // leaves a row, but doesn't crash; remove -1 and it removes all rows and crashes
...and...
for (int i = 0; i<= ui->myTable->rowCount(); i++) { ui->myTable->removeRow(0); } // leaves a row, but doesn't crash; set i = -1 and it removes all rows and crashes
Did I stumble upon a huge showstopper bug in Qt 5.5?
EDIT: Hang on...may be because later on in the code I'm trying to select a row that no longer exists....
-
@maximo
Hello.
I rapidly draw an application designing a QTableView with QDesigner adding 2 rows and a PushButton with the following code
void MainWindow::on_pushButton_clicked()
{
ui->tableWidget->model()->removeRows(0,ui->tableWidget->rowCount());
}I have no crash.
In fact you use to remove the model rows with the rowCount of the Table. Perhaps you may try to use ui->myTable->model()->removeRows(0,ui->myTable->model()->rowCount()); to be coherent.
But is the model of your QTableWidget or your QTableWidget itself connected to another QObjet?
-
@Olivier-Ronat Thanks for checking, Olivier. Sorry to detain you to do that when I found it was my own fault. However, I learned some things and got a little better at debugging in Qt Creator.
I found the cause. I had a signal attached to the selectionModel of each row. Here's how I found out (note, I'm a newbie). On my Mac, I got the crash report and clicked a button to see what it was. This showed a callstack. I was able to see that it was complaining about setting
text()
fromon_SelRow
, and then I recalled that I had a signal connected on rows. So, I changed my code like so:ui->myTable->selectionModel()->disconnect(); ui->myTable->model()->removeRows(0,ui->myTable->rowCount());
I was then able to remove rows without a crash. I just need to remember to add the signal back to the table when I add rows again.
P.S.
You may also be wondering why I was intercepting a selected row. The reason was because then I could show a box above the table that showed the item detail in an easier to read format than having to scroll horizontally in the table to see all the columns. I was doing it like so:
connect(ui->myTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(on_SelRow(QModelIndex,QModelIndex)));
...and then I had a class method like so:
void MainWindow::on_SelRow(QModelIndex oCurrRow, QModelIndex oPrevRow) { QTableWidget *t = dynamic_cast<QTableWidget *>(QObject::sender()->parent()); int y = oCurrRow.row(); // do something with t (table) and y (row index) variables }