Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableManager Removing All Rows Crashes Application with Bad Access

QTableManager Removing All Rows Crashes Application with Bad Access

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.5qtablewidgetremoveremoverowrow
3 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maximo
    wrote on 28 Oct 2015, 06:08 last edited by maximo
    #1

    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....

    O 1 Reply Last reply 28 Oct 2015, 06:28
    0
    • M maximo
      28 Oct 2015, 06:08

      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....

      O Offline
      O Offline
      Olivier Ronat
      wrote on 28 Oct 2015, 06:28 last edited by Olivier Ronat
      #2

      @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?

      M 1 Reply Last reply 28 Oct 2015, 06:55
      0
      • O Olivier Ronat
        28 Oct 2015, 06:28

        @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?

        M Offline
        M Offline
        maximo
        wrote on 28 Oct 2015, 06:55 last edited by
        #3

        @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() from on_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
        }
        
        1 Reply Last reply
        0

        3/3

        28 Oct 2015, 06:55

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved