Important: Please read the Qt Code of Conduct -

[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.

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

  • Qt Champions 2017

    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;

  • Sorry Dheerendra this is my actual line


    Below are the values present in Qtstringlist = 2103 = 2204 = 2296 = 7485 = 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,
    //Do Some Operation
    //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" = 2013@
    comparing these qbytearray and qstring data the above mentioned if condition should return true.

  • Qt Champions 2017

    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() << << " 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. :)

  • Qt Champions 2017

    You can put this question to SOLVED state.

Log in to reply