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 compare between all values in two columns and state result in the column 3
QtWS25 Last Chance

How to compare between all values in two columns and state result in the column 3

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.7k Views
  • 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.
  • R Offline
    R Offline
    reem9611
    wrote on last edited by
    #1

    I have two column that contains values on the third column in need to state result after comparing between the two values in column 1 and 2.

    I can get th values from cell contant using the following line of code
    ui->tableWidget->item(0,0)->text();

    I tries as first to compare between two cells (0,0) and (0,1) and thats work for me

    how i can implement that to all values (raw,0)(raw,1) ?

    qxoz: moved from QtQuick forum

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Maybe
      @int rowCount = ui->tableWidget->rowCount();
      for(int i = 0; i<rowCount; i++)
      {
      ui->tableWidget->item(i,0)->text();
      //...
      }@
      can help?

      1 Reply Last reply
      0
      • EddyE Offline
        EddyE Offline
        Eddy
        wrote on last edited by
        #3

        Hi reem 9611,

        Here is some code that compares and stores the result :
        @
        void MainWindow::compareValues(QTableWidgetItem * item)
        {
        const int lastCol = myTable->columnCount() - 1;
        if (item && item->column() == lastCol)
        return;

        for (int row = 0; row < myTable->rowCount(); ++row) {
            QString equal ;
            QTableWidgetItem *It1 = myTable->item(row, 0);
            QTableWidgetItem *It2 = myTable->item(row, 1);
            int val1 = It1->text().toInt();
            int val2 = It2->text().toInt();
        
            val1 == val2 ? equal = "Y" : equal = "N" ;
            myTable->item(row , lastCol)->setText(equal);
        }
        

        }
        @

        happy coding

        Qt Certified Specialist
        www.edalsolutions.be

        1 Reply Last reply
        0
        • R Offline
          R Offline
          reem9611
          wrote on last edited by
          #4

          [quote author="Eddy" date="1395741763"]Hi reem 9611,

          Here is some code that compares and stores the result :
          @
          void MainWindow::compareValues(QTableWidgetItem * item)
          {
          const int lastCol = myTable->columnCount() - 1;
          if (item && item->column() == lastCol)
          return;

          for (int row = 0; row < myTable->rowCount(); ++row) {
              QString equal ;
              QTableWidgetItem *It1 = myTable->item(row, 0);
              QTableWidgetItem *It2 = myTable->item(row, 1);
              int val1 = It1->text().toInt();
              int val2 = It2->text().toInt();
          
              val1 == val2 ? equal = "Y" : equal = "N" ;
              myTable->item(row , lastCol)->setText(equal);
          }
          

          }
          @

          happy coding[/quote]

          Thanks a lot I .. when I implement your code the app didn't respond..

          I tried that
          @void MainWindow::GenerateRes(){

          int row=0;
          int val1 = ui->tableWidget->item(row,0)->text().toInt();
          int val2 = ui->tableWidget->item(row,1)->text().toInt();
          
          while (row < ui->tableWidget->rowCount()-1) {
          
                 QString Result ;
          
          
          
                 val1 == val2 ? Result = "Y" : Result = "N" ;
          
               QTableWidgetItem *Item = new QTableWidgetItem(Result);
          
                 ui->tableWidget->setItem(row,2,Item);
                 row++;
          
          
                }
          

          }
          @
          But I get the result for the only first result .. and the rows below get the same result for the first cell

          so if the values are
          First column 1,2,3
          second column 2,2,3
          result will be N,N,N

          1 Reply Last reply
          0
          • EddyE Offline
            EddyE Offline
            Eddy
            wrote on last edited by
            #5

            You're welcome

            The code I gave you works on my system. I don't have the same data as yours of course. So there must be some error in your logic. The best way to find out is to debug and see what values are used in your program

            Qt Certified Specialist
            www.edalsolutions.be

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              zhebulonn
              wrote on last edited by
              #6

              Hi,

              I haven't tried, but I think it's because of the "-1" in :

              @while (row < ui->tableWidget->rowCount()-1)@

              Because oh this, you never read the last row.

              And you don't update val1 and val2 in your while loop. You always compare the datas of the first row.

              1 Reply Last reply
              0
              • EddyE Offline
                EddyE Offline
                Eddy
                wrote on last edited by
                #7

                [quote author="zhebulonn" date="1395842105"]Hi,

                I haven't tried, but I think it's because of the "-1" in :

                @while (row < ui->tableWidget->rowCount()-1)@

                Because oh this, you never read the last row.
                [/quote]

                That's not the cause of the error. It can be usefull in case you want to use the last row to sum / count things. eg. how many "Y"s

                Qt Certified Specialist
                www.edalsolutions.be

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  reem9611
                  wrote on last edited by
                  #8

                  thanks Eddy,
                  I use the debug and that help
                  the logic of code is correct :

                  1
                  1
                  "Y"
                  0xd1163b0
                  2
                  2
                  "Y"
                  0xd1163d0
                  3
                  3
                  "Y"
                  0xd116390

                  but the app crash and show a message that app stopped working ..I think his happen becuase of this line of code row<ui->tableWidget->rowCount().

                  when it read empty cells the app stopped working ..in my code I specified row to the fixed number .

                  any hint of how to handle this error?

                  [quote author="Eddy" date="1395823936"]You're welcome

                  The code I gave you works on my system. I don't have the same data as yours of course. So there must be some error in your logic. The best way to find out is to debug and see what values are used in your program[/quote]

                  1 Reply Last reply
                  0
                  • EddyE Offline
                    EddyE Offline
                    Eddy
                    wrote on last edited by
                    #9

                    bq. when it read empty cells the app stopped working

                    So you found the error.
                    @
                    val1 == val2 ? Result = "Y" : Result = "N" ;
                    @

                    Make sure you have a value in there. Or test if there is a value and only then compare the values.

                    Qt Certified Specialist
                    www.edalsolutions.be

                    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