[SOLVED] testing if returned string == another string

  • Hello,

    In a method i wrote, i need to test if a QString passed to my method == one QString returned by a method used in this portion of code...

    I put some qDebug() to debug some variables.
    It seems that "input" is ok and my value returned by parseResponse() is also ok.
    In other words, i have a case where input == parseResponse() but the following line never fires :

    @qDebug() << "entering switch statement" << i;@

    here is what i have so far :

    @void Matrix::viewTiedOutputs(QString input)
    qDebug() << "view tied outputs for input" << input;
    for(int out = 1; out < 5; out++)
    qDebug() << "sendQuery for output " << out << "returned : " << parseResponse(dxpClient->sendQuery(QString::number(out) + "!"));
    if(parseResponse(dxpClient->sendQuery(QString::number(out) + "!")) == input)
    qDebug() << "looping switch statment" << out;
    case 1: ui->dxp_out_1->setChecked(true); break;
    case 2: ui->dxp_out_2->setChecked(true); break;
    case 3: ui->dxp_out_3->setChecked(true); break;
    case 4: ui->dxp_out_4->setChecked(true); break;
    qDebug() << "input " << input << " tied to output " << out;

    parseResponse is as follow :

    QString Matrix::parseResponse(QString msg)
    return msg.remove("\n", Qt::CaseInsensitive);

    And console feedback for qDebug() outputs :

    bq. view tied outputs for input "1"
    sendQuery for output 1 returned : "1"
    sendQuery for output 2 returned : "3"
    sendQuery for output 3 returned : "E13"
    sendQuery for output 4 returned : "E13"

    thanks for your contribution.

  • What data type does parseResponse() returns?

    Bear in mind that comparing int to QString (or any other string type) will have unexpected results. If you are seeing they are the same in your screen but the comparison doesn't work then that's probably the cause.

  • Are your sure that you don't add spaces or other invisible characters?
    Can you please post what the qDebug output of input and the parse output is?

  • Hi,
    Couple of issues I see in your code:

    • Never ever use the variable i or x, or y or whatever single character name. This is really confusing and not recommended for good readable code. Use a proper coding standard. Don't forget that your code will probably will be read a thousand time and only written ones. Don't be lazy!
    • When using Debug is is wise to not call a function directly if the return code of that function also needs to be used later on. Store the return function in a local variable and use that consistently in the function. This way all parts of the function use the same input related values. (in this case the parseResponse function returned string).
    • Use the debugger to check out any differences (when using local variables this is possible).

    Hope these tips help a bit to get to the problem. The QString == operator should work otherwise.

  • Hi,

    Thanks for the quick answers.

    ok changed "i" for "out", thanks for the suggestion.

    i updated the first post with more info.

  • Hello,

    Finally i got it to work by cleaning my response with QString::trimmed()

    @ if(dxpClient->sendQuery(QString::number(out) + "!").trimmed() == input)

  • Good to hear that it works.

    It might be a religious question but run variable in loops can be named i, j, k (and especially i, j, k). out does not have more information and it is very common to use i, j, and k for run variables.

    For all other variables Jeroen is right of course!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.