0

When I supply QString::compare with two strings:

for(int i = 0; i < ui->tableWidget->rowCount(); i++
{
    // assuming searchDialog is a custom-made class with method getResultMap
    QMap<QString, int> result = searchDialog->getResultMap();
    QString phrase = result.keys().at(0);
    int itemFound = 1;    

    if(itemFound == QString::compare(ui->tableWidget->item(i, searchByColumn)->text(),  
                                                       phrase, 
                                                       Qt::CaseInsensitive)) == 0)
    {
         // Enters here even though the strings differ
    }
}

It always returns 0 even though the strings are e.g:

"Car"
"Horse"
"Human"

However when I do such condition:

if(ui->tableWidget->item(i, searchByColumn)->text() == phrase)
{
    qDebug() << "Entered the condition";
}

Than the strings appear to be different (it does not enter the condition).

What could be the possible reason?

Kokos34
  • 67
  • 1
  • 11
  • Your question is a bit unclear. In the first code example, where does `i` come from? And why should we expect the condition to evaluate true? Can you maybe explain what behaviour you intend to achieve with the condition in the `if` statement? – bweber Mar 06 '17 at 15:12
  • I wanted to avoid adding too many lines of code. i is the int variable that traverses the tableWidget row by row: for(int i = 0; i < ui->tableWidget->rowCount(); i++). As far as the condition is concerned, I wanted to check whether the strings are dfferent (as intended) or actually something in getting QString's is wrong. Since the "Entered the condition" is displayed only in case of the same rows, than there is something wrong with QStrings::compare method. – Kokos34 Mar 06 '17 at 15:16
  • Sorry, but that's not making it more clear. From what I see you compare the content of a table cell with some variable `phrase` (assumingly a QString). Now, we don't have any information on what values the strings being compared have, so how are we supposed to draw any conclusion? However, I assume that the condition `if(itemFound == QString::compare(ui->tableWidget->item(i, searchByColumn)->text(), phrase, Qt::CaseInsensitive)) == 0)` might not acutally do what you think it should do, so it would help if you could provide an explanation of what you think it is supposed to do. – bweber Mar 06 '17 at 15:24
  • phrase is a class field of type QString. I updated the post a bit. Maybe it makes a bit more sense now. – Kokos34 Mar 06 '17 at 15:28
  • Ok, I assume that the condition `if(itemFound == QString::compare(ui->tableWidget->item(i, searchByColumn)->text(), phrase, Qt::CaseInsensitive)) == 0)` is the problem. The condition as it is written now returns `true` unless the return value of `QString::compare` is exactly `1`. Based on your explanation I assume that you wanted to have something like `if(true == (QString::compare(ui->tableWidget->item(i, searchByColumn)->text(), phrase, Qt::CaseInsensitive)) == 0))` instead. – bweber Mar 06 '17 at 15:33
  • But according to documentation method [static] int QString::compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive): "Compares s1 with s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.". Shouldn't it return 0 if the strings are same? – Kokos34 Mar 06 '17 at 15:38
  • 1
    Yes, it does. That's what the part `QString::compare(ui->tableWidget->item(i, searchByColumn)->text(), phrase, Qt::CaseInsensitive) == 0` of the condition checks. If the result of this comparison is true, the strings are equal. In your original example it was compared to `1` due to the missing parantheses around the second comparison. – bweber Mar 06 '17 at 15:40
  • Of course. I missed the parenthesis. Thanks very much for patience. – Kokos34 Mar 06 '17 at 15:44

0 Answers0