[Solved] Comparing QString and QByteArray



  • Hello everyone, I have a problem comparing Qstring and Qbytearray data, Can anyone help me with this please.!!

    @
    QStringList str_id;

    QByteArray readData;

    if (readData.contains(str_id[i].toUtf8())) //This is the part of code where my error exists.

    @
    Error:
    no matching function for call to 'QByteArray::contains(QString&)'



  • What is 'i' there in str_id[i].toUtf8() ? Just try with this. Based on this you can fix the issue.

    @ QStringList list;
    list<< "1 "<<"2";
    QByteArray data;
    data.contains(list[1].toUtf8());@



  • Sorry Dheerendra this is my actual line

    @if(readData.contains(str_id.at(i)))@

    Below are the values present in Qtstringlist
    str_id.at(1) = 2103
    str_id.at(2) = 2204
    str_id.at(3) = 2296
    str_id.at(4) = 7485
    str_id.at(5) = 9375

    and readData is a QByteArray which consists of one of the following at a time
    @"ID : 2013 Heart Beat : 72 Movement : yes"
    "ID : 2048 Heart Beat : 75 Movement : yes"
    "ID : 2169 Heart Beat : 73 Movement : yes"
    "ID : 2204 Heart Beat : 72 Movement : yes"@

    So I have to compare the qstringlist element each at a time with the Qbytearray data, i have written for loop for doing this hence 'i' variable is used like below,
    @
    for(i=0;i<n;i++)
    {
    if(readData.contains(str_id.at(i)))
    {
    //Do Some Operation
    }
    else
    //Do Some Operation
    }@

    and n is the Qstringlist size which is calculated through size function i.e "str_id.size()"

    Can you help me out with the compare function please.!!

    The following is the actual scenario which I have to do with this code,
    @readData = "ID : 2013 Heart Beat : 72 Movement : yes"
    str_id.at(1) = 2013@
    comparing these qbytearray and qstring data the above mentioned if condition should return true.



  • Not sure why are converting data to QByteArray. I remember you are reading data from file and making it ByteArray. I feel which converting to ByteArray is not required.

    You can try something like this.

    @ QByteArray readData = "ID : 2013 Heart Beat : 72 Movement : yes";
    qDebug() << readData.data() << " He ="<<readData;
    QByteArray dat = "2013";
    QString data1= "2013";
    if (dat.contains(data1.toUtf8())){
    qDebug() << "What man "<<endl;
    }else {
    qDebug() << "No man"<<endl;
    }@



  • Thanks for your simple sample code Dheerendra. It worked and I modified my project code with it. Thanks a lot once again. :)



  • You can put this question to SOLVED state.


Log in to reply
 

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