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. How to read data from a column of QTableWidget ?
Forum Updated to NodeBB v4.3 + New Features

How to read data from a column of QTableWidget ?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.5k 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.
  • A Offline
    A Offline
    Aromakc
    wrote on last edited by
    #1

    I have written this code to read data from table containing 4 column (4th being amount). I have error when reading data from QTableWidget, table name(table). After pressing net Pushbutton, program crashes.

    void MainWindow::on_net_clicked()
    {
    int total=0;
    int row=ui->table->rowCount();

    for(int i=1;i<=row;i++){
        QTableWidgetItem* a=ui->table->item(i,4);
        int b=a->text().toInt();
         total = (total + b);
    }
    QString gross= QString::number(total);
    ui->le_gross->setText(gross);
    

    }

    Screenshot (189).png

    Screenshot (188).png

    JonBJ 1 Reply Last reply
    0
    • A Aromakc

      I have written this code to read data from table containing 4 column (4th being amount). I have error when reading data from QTableWidget, table name(table). After pressing net Pushbutton, program crashes.

      void MainWindow::on_net_clicked()
      {
      int total=0;
      int row=ui->table->rowCount();

      for(int i=1;i<=row;i++){
          QTableWidgetItem* a=ui->table->item(i,4);
          int b=a->text().toInt();
           total = (total + b);
      }
      QString gross= QString::number(total);
      ui->le_gross->setText(gross);
      

      }

      Screenshot (189).png

      Screenshot (188).png

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Aromakc said in How to read data from a column of QTableWidget ?:

      ui->table->item(i,4)

      Where do you think that 4 point to? I only see 4 columns in your screenshot, not 5....

          QTableWidgetItem* a=ui->table->item(i,4);
          int b=a->text().toInt();
      

      It "crashes" (on a->text()) because you do not test whether a == nullptr, which it will do, and you should always test for....

      A 1 Reply Last reply
      3
      • JonBJ JonB

        @Aromakc said in How to read data from a column of QTableWidget ?:

        ui->table->item(i,4)

        Where do you think that 4 point to? I only see 4 columns in your screenshot, not 5....

            QTableWidgetItem* a=ui->table->item(i,4);
            int b=a->text().toInt();
        

        It "crashes" (on a->text()) because you do not test whether a == nullptr, which it will do, and you should always test for....

        A Offline
        A Offline
        Aromakc
        wrote on last edited by
        #3

        @JonB
        I will try what are you suggesting too. I have tried this method and looks like it is working. Is it proper?
        //I didn't know column starts at 0; thought it start with 1 :""

        void MainWindow::on_net_clicked()
        {
        int total=0;
        //int row=ui->table->rowCount();

        for(int i=0;i<ui->table->rowCount();i++){
          int a=ui->table->item(i,3)->text().toInt();
          total=total+a;
        }
        QString gross= QString::number(total);
        ui->le_gross->setText(gross);
        

        }

        JonBJ Pl45m4P 2 Replies Last reply
        0
        • A Aromakc

          @JonB
          I will try what are you suggesting too. I have tried this method and looks like it is working. Is it proper?
          //I didn't know column starts at 0; thought it start with 1 :""

          void MainWindow::on_net_clicked()
          {
          int total=0;
          //int row=ui->table->rowCount();

          for(int i=0;i<ui->table->rowCount();i++){
            int a=ui->table->item(i,3)->text().toInt();
            total=total+a;
          }
          QString gross= QString::number(total);
          ui->le_gross->setText(gross);
          

          }

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Aromakc said in How to read data from a column of QTableWidget ?:

          //I didn't know column starts at 0; thought it start with 1 :""

          We are in C/C++ realm, not Pascal/Visual Basic :)

          Your code will now work. Provided you have >= 4 columns.

          1 Reply Last reply
          0
          • A Aromakc

            @JonB
            I will try what are you suggesting too. I have tried this method and looks like it is working. Is it proper?
            //I didn't know column starts at 0; thought it start with 1 :""

            void MainWindow::on_net_clicked()
            {
            int total=0;
            //int row=ui->table->rowCount();

            for(int i=0;i<ui->table->rowCount();i++){
              int a=ui->table->item(i,3)->text().toInt();
              total=total+a;
            }
            QString gross= QString::number(total);
            ui->le_gross->setText(gross);
            

            }

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @Aromakc said in How to read data from a column of QTableWidget ?:

            thought it start with 1

            I'm wondering, how you got this far without facing any errors or crashes earlier :o

            In most of the programming languages indices (of tables, arrays, vectors or other container classes) start at 0.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            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