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. Unable to display items with QWidgetTable

Unable to display items with QWidgetTable

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.2k Views 1 Watching
  • 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.
  • RustR Offline
    RustR Offline
    Rust
    wrote on last edited by Rust
    #1

    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!

    RatzzR miclandM 2 Replies Last reply
    0
    • RustR Rust

      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!

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #2

      @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

      --Alles ist gut.

      1 Reply Last reply
      2
      • RustR Rust

        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!

        miclandM Offline
        miclandM Offline
        micland
        wrote on last edited by
        #3

        @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, ....)`;

        RustR 1 Reply Last reply
        2
        • miclandM micland

          @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, ....)`;

          RustR Offline
          RustR Offline
          Rust
          wrote on last edited by Rust
          #4

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

          miclandM 1 Reply Last reply
          0
          • RustR Rust

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

            miclandM Offline
            miclandM Offline
            micland
            wrote on last edited by
            #5

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

            RustR 1 Reply Last reply
            0
            • miclandM micland

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

              RustR Offline
              RustR Offline
              Rust
              wrote on last edited by
              #6

              @micland

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

              miclandM 1 Reply Last reply
              0
              • RustR Rust

                @micland

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

                miclandM Offline
                miclandM Offline
                micland
                wrote on last edited by
                #7

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

                RustR 1 Reply Last reply
                1
                • miclandM micland

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

                  RustR Offline
                  RustR Offline
                  Rust
                  wrote on last edited by
                  #8

                  @micland

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

                  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