Unable to display items with QWidgetTable



  • I'm having a couple of issues with QWidgetTable, i've sectioned them in two lines.

    1. I'm unable to append items to a QWidgetTable, i've wrote the following code:
            ui->Tabela->insertRow(ui->Tabela->rowCount());
            ui->Tabela->setItem(ui->Tabela->rowCount(), 1, new QTableWidgetItem(_cod_produto));
            ui->Tabela->setItem(ui->Tabela->rowCount(), 2, new QTableWidgetItem(_nome_produto));
            ui->Tabela->setItem(ui->Tabela->rowCount(), 3, new QTableWidgetItem(_quantidade));
            ui->Tabela->setItem(ui->Tabela->rowCount(), 4, new QTableWidgetItem(_quantidade_stock));
            ui->Tabela->setItem(ui->Tabela->rowCount(), 5, new QTableWidgetItem(_preco));
    

    But all it does is insert an empty row at the end. I thought setItem would append the item to the table. For extra info, the _cod_produto, _nome_produto, _quantidade and _quantidade_stock are QString's.

    1. I need to read values again from the table, i've wrote the following:
    while(i++ < ui->Tabela->rowCount()){
            qDebug() << "From table: " << ui->Tabela->item(i,0)->text() << endl;
            _cod_produto = ui->Tabela->item(i,0)->text();
            _quantidade_stock = ui->Tabela->item(i,2)->text();
            _quantidade = ui->Tabela->item(i,3)->text();
            _preco = ui->Tabela->item(i,4)->text();
    ...
    ...
    ...
    
    

    They return empty, i assume because every row in the table is blank and not due to any mistake i made while reading those values.

    I would be very thankful for any help possible with this, i apologise for my rudimentary skills with Qt beforehand, and thank you in advance for any help!



  • @Rust
    Doc says Inserts an empty row into the table at row.
    Instead you can use setRowCounthttp://doc.qt.io/qt-4.8/qtablewidget.html#rowCount-prop



  • @Rust said:

            ui->Tabela->insertRow(ui->Tabela->rowCount());
            ui->Tabela->setItem(ui->Tabela->rowCount(), 1, new QTableWidgetItem(_cod_produto));
    

    I did not try to compile your code but the last row has index rowCount()-1 (index starts at 0), so your setItem(...)-calls refer to an unexisting row (same for columns: your starting at index 1 - is this really what you want?). Try to call setItem(ui->Tabela->setItem(ui->Tabela->rowCount()-1, ....)`;



  • @micland

    Indeed i fixed that. QTableWidget now works as expected.

    But now my problem is when i'm attempting to delete a row, iterating through all of them to find a matching field value to delete a row. My program crashes at the following loop...

        qDebug() << "Now iterating through rows..." << endl;
        //iterate through indexes to remove item
        while(i <= ui->Tabela->rowCount()){
            qDebug() << "Row: " << ui->Tabela->item(i,0)->text() << endl;
            qDebug() << "Current iteration index:" << i << endl;
            if(_cod_produto == (ui->Tabela->item(i,0)->text())){
                QString _preco = ui->Tabela->item(i, 4)->text();
                if(_preco.isEmpty()){
                    qDebug() << "Price is empty, will now return and ignore action" << endl;
                    
                    return;
                }
                QString _preco_total = ui->PrecoTotal->toPlainText();
                _preco_total = QString::number(_preco_total.toInt() - _preco.toInt());
                ui->PrecoTotal->clear();
                ui->PrecoTotal->appendPlainText(_preco_total);
                ui->Tabela->removeRow(i);
    
                break;
            }
            i++;
        }
    

    Now iterating through rows... is printed onto the console, but...

    qDebug() << "Row: " << ui->Tabela->item(i,0)->text() << endl;
    

    never gets to execute and everything crashes before it at

    while(i <= ui->Tabela->rowCount()){
    

    i is an int, rowCount() returns an int, or is it an unsigned it and it's because of that that everything's crashing?

    Note: The table has values filled in when calling the loop.



  • @Rust said:

        qDebug() << "Now iterating through rows..." << endl;
        //iterate through indexes to remove item
        while(i <= ui->Tabela->rowCount()){
    

    Same problem: You're trying to access an unexisting row! Use operator < instead of <= to ensure that i is less than the rowCount and not "less or equal".
    (And just to be sure: do you initialize i = 0 before entering the while loop? this is missing in the example snippet above...)



  • @micland

    Indeed it's missing, i set it to i = 1 before the loop. I'll give your hints a try.



  • @Rust said:

    Indeed it's missing, i set it to i = 1 before the loop. I'll give your hints a try.

    You have to set it to 0 and increment until rowCount-1. The indexes start with 0 so the last row has index rowCount-1. (a table with 4 rows has a rowCount of 4 and the indexes 0, 1, 2, 3 - same way as arrays are indexed and accessed).



  • @micland

    Managed to fix the issue with your hints. Thank you!


Log in to reply
 

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