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.4k 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 9 Sept 2020, 11:50 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

    J 1 Reply Last reply 9 Sept 2020, 11:52
    0
    • A Aromakc
      9 Sept 2020, 11:50

      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

      J Online
      J Online
      JonB
      wrote on 9 Sept 2020, 11:52 last edited by JonB 9 Sept 2020, 11:54
      #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 9 Sept 2020, 12:10
      3
      • J JonB
        9 Sept 2020, 11:52

        @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 9 Sept 2020, 12:10 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);
        

        }

        J P 2 Replies Last reply 9 Sept 2020, 12:12
        0
        • A Aromakc
          9 Sept 2020, 12:10

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

          }

          J Online
          J Online
          JonB
          wrote on 9 Sept 2020, 12:12 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
            9 Sept 2020, 12:10

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

            }

            P Offline
            P Offline
            Pl45m4
            wrote on 9 Sept 2020, 12:22 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

            1/5

            9 Sept 2020, 11:50

            • Login

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