How to compare between all values in two columns and state result in the column 3
-
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
-
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 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 cellso if the values are
First column 1,2,3
second column 2,2,3
result will be N,N,N -
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.
-
[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
-
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"
0xd116390but 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]