<SOLVED> Adding Row To QT Table Widget Clears Another Row



  • I created a function below that adds a row to my table. However, when I tie it to a button click it works the first time, but after that it clears another row in the table when it populates the new row.

    Fist Click
    !http://i.stack.imgur.com/jp1St.png!

    Second Click
    !http://i.stack.imgur.com/R4rYr.png!

    @void MainWindow::addRow(int id)
    {
    Inventory i(id);

    QTableWidgetItem *newItem1 = new QTableWidgetItem(QString::fromStdString(i.getItem().getItem_Name()));
    QTableWidgetItem *newItem2 = new QTableWidgetItem(QString::fromStdString(i.getItem().getCategory().getCatName()));
    QTableWidgetItem *newItem3 = new QTableWidgetItem(QString::fromStdString(cn.dbl_to_str(i.getQnty())));
    QTableWidgetItem *newItem4 = new QTableWidgetItem(QString::fromStdString(i.getLoc().getLocName()));
    QTableWidgetItem *newItem5 = new QTableWidgetItem();
    newItem5->setData(Qt::UserRole,QVariant(i.getInv_ID()));
    
    QIcon qi;
    qi.addFile&#40;QString::fromStdString("red_error_warning_icon.svg"&#41;&#41;;
    newItem5->setIcon(qi);
    
    int j = ui->mainTable->rowCount();
    cout << "i.getInv_ID(): " << i.getInv_ID() << endl;
    cout << "newItem5->setData: " << newItem5->data(Qt::UserRole).toString().toStdString() << endl;
    cout << "j: " << j << endl;
    
    ui->mainTable->insertRow(j);
    ui->mainTable->setItem(j,0,newItem1);
    ui->mainTable->setItem(j,1,newItem2);
    ui->mainTable->setItem(j,2,newItem3);
    ui->mainTable->setItem(j,3,newItem4);
    ui->mainTable->setItem(j,4,newItem5);
    

    }@



  • I found that I had column sort turned on in the ui file which was affecting my item inserts.
    Here is the working code for adding rows which disables sorting while it adds items.
    @
    void MainWindow::addRow(int id)
    {
    ui->mainTable->setSortingEnabled(false);
    Inventory i(id);

    QTableWidgetItem *newItem1 = new QTableWidgetItem(QString::fromStdString(i.getItem().getItem_Name()));
    QTableWidgetItem *newItem2 = new QTableWidgetItem(QString::fromStdString(i.getItem().getCategory().getCatName()));
    QTableWidgetItem *newItem3 = new QTableWidgetItem(QString::fromStdString(cn.dbl_to_str(i.getQnty())));
    QTableWidgetItem *newItem4 = new QTableWidgetItem(QString::fromStdString(i.getLoc().getLocName()));
    QTableWidgetItem *newItem5 = new QTableWidgetItem();
    newItem5->setData(Qt::UserRole,QVariant(i.getInv_ID()));
    
    QIcon qi;
    qi.addFile&#40;QString::fromStdString("red_error_warning_icon.svg"&#41;);
    newItem5->setIcon(qi);
    
    int j = ui->mainTable->rowCount();
    
    ui->mainTable->insertRow(j);
    ui->mainTable->setItem(j,0,newItem1);
    ui->mainTable->setItem(j,1,newItem2);
    ui->mainTable->setItem(j,2,newItem3);
    ui->mainTable->setItem(j,3,newItem4);
    ui->mainTable->setItem(j,4,newItem5);
    ui->mainTable->setSortingEnabled(true);
    }@

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.