1

I have a QTableWidget with 3 columns and 1000 rows (depends on the number of fetched items from the database ).

I also have a "Reload" Button to reload all the items from the database.

In "Reload" button, I want to delete all the rows from QTableWidget before loading items from the database.

Deleting all rows from the QTableWidget crashes the application when rowcount = 1. I am using following logic to delete all the rows.

if( ui->tableWidget->rowCount() > 0)
{
  ui->tableWidget->setRowCount(0);
}

also tried below logic

 while (ui->tableWidget->rowCount() > 0)
{
  ui->tableWidget->removeRow(0);
 }

My application crashes using either of the logic when rowCount = 1. It happens only when i build my application in release mode while it works fine in debug mode.

Can i get any idea why my application is getting crash.

skg
  • 948
  • 2
  • 19
  • 35
  • 1
    Are you sure your program crashes at those lines? – Robert Jan 13 '15 at 10:18
  • @Robert Yes, i did manual debugging by commenting the lines in code. Also I used QDebug to print rowcount after removingRow(0). It displays until rowCount = 1 and then it crashes. – skg Jan 13 '15 at 10:26
  • That not necessarily means that the program crashes exactly at those lines, what I'm thinking about is that you probably access (later on) the first element of the tablewidget, when you remove it you get an access violation. ...could be, just trying to help ^^ – Robert Jan 13 '15 at 10:29
  • Try use `ui->tableWidget->clearContents()` instead of `setRowCount(0)` – Meefte Jan 13 '15 at 10:34
  • @Meefte...I tried clearContents() also. Its crashes. :( – skg Jan 13 '15 at 11:01
  • @thuga, this issue is occuring only in release build while works fine in debug build. – skg Jan 13 '15 at 11:23
  • Check everywhere where you use `ui->tableWidget`, try to comment it out and see if it ever stops crashing. – thuga Jan 13 '15 at 11:24

1 Answers1

1

I had the same problem, from one day to another it stopped working and I couldn't find what part of my code was making my program crash.

My program was used for reading files from a folder and uploading the file information into a QTableWidget, and I was also using cellWidgets for some information.

These are the problems that came to my mind, you should check that they're not happening to you:

  • The program was modifying the table all the time, so maybe i was trying to access an empty table. So I commented the lines where I was calling the table row and debugged it, but this wasn't a problem.

  • It could be a debugger problem, so I tried building it in Release and Profile mode, but still didn't work.

  • I could be having a problem when trying to access the cellWidget information, so I checked and I wasn't trying to access it.

  • Maybe the problem was when I was trying to eliminate a row that had a cellWidget, so I tried using ui->tableWidget->clearContents() before setting the row count to 0, (Didn't work)

After trying all of these I came upon this post. And basically I replaced setRowCount(0) with it's internal implementation ui->table->model()->removeRows(0,ui->table->rowCount());, debugged it and it worked.