Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qtablewidget reset and new data adding crashes



  • Hi,

    I am creating a QTablewidget and populating data. Always first run is ok.

    When I try to clear / remove rows (basicly try to start over with fresh one) it is crashing at below line:

    in : qtablewidget.h

    inline QString text() const
            { return data(Qt::DisplayRole).toString(); }
        inline void setText(const QString &text);
    

    my code is :

    QString current_personel = "";
            qint16 row1000 = 0;
    
            ui->personel_tablo->clearContents();
    int  totalRow = ui->personel_tablo->rowCount();
        
        for ( int i = 0; i < totalRow ; ++i )
        {
               ui->personel_tablo->removeRow(i);
               qDebug() << "deleted " << i << " row " << endl;
    
        }
            QCoreApplication::processEvents();
            ui->personel_tablo->repaint();
    ui->personel_tablo->insertRow(0);
    
                    current_personel = enrolment_Db[i].person_name;
    //                ui->personel_tablo->insertRow(0);
    
                    QTableWidgetItem* name      = new QTableWidgetItem;
                    QTableWidgetItem* photo     = new QTableWidgetItem;
                    QTableWidgetItem* category  = new QTableWidgetItem;
                    QTableWidgetItem* path      = new QTableWidgetItem;
                    QTableWidgetItem* status    = new QTableWidgetItem;
    
                    name->setFlags(category->flags() ^ Qt::ItemIsEditable);
                    photo->setFlags(category->flags() ^ Qt::ItemIsEditable);
                    path->setFlags(category->flags() ^ Qt::ItemIsEditable);
                    status->setFlags(category->flags() ^ Qt::ItemIsEditable);
    
                    name->setText(enrolment_Db[i].person_name);
                    photo->setData(Qt::DecorationRole ,enrolment_Db[i].person_foto.scaled(150,150,Qt::KeepAspectRatio));
                    category->setText(enrolment_Db[i].category);
                    path->setText(enrolment_Db[i].file);
                    status->setText(enrolment_Db[i].face_indexed_status);
    
                    if (enrolment_Db[i].category.toUpper() == "VIP"){
                        category->setForeground(QColor::fromRgb(255,0,0));
                        name->setForeground(QColor::fromRgb(255,0,0));
    
                    }
                    ui->personel_tablo->setItem(0 , 0 , name);
                    ui->personel_tablo->setItem(0 , 1 , photo );
                    ui->personel_tablo->setItem(0 , 2 , category);
                    ui->personel_tablo->setItem(0 , 3 , status);
                    ui->personel_tablo->setItem(0 , 4 , path);
                    ui->personel_tablo->repaint();
                    qApp->processEvents();
    

  • Lifetime Qt Champion

    Hi
    Its a common gotcha
    You take the totalRow first.
    Then you remove items. Its means size changes from when you took it.
    so then index (i) becomes bigger than the actual list at some point.

    anyway, just use
    http://doc.qt.io/qt-5/qtablewidget.html#clear



  • Also I figured that any connected signal to the table cause troubles.

    I disconnect all signals from the table before inserting items now its fine.

    But still cant delete all items before inserting new ones.

    Will update according to your suggestions.

    thx


Log in to reply