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. Qtablewidget reset and new data adding crashes
Forum Update on Monday, May 27th 2025

Qtablewidget reset and new data adding crashes

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 933 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.
  • R Offline
    R Offline
    RahibeMeryem
    wrote on last edited by
    #1

    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();
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      3
      • R Offline
        R Offline
        RahibeMeryem
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

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