Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Comparing QString and QByteArray

[Solved] Comparing QString and QByteArray

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    srikanth.koorma
    wrote on 11 Sept 2014, 08:51 last edited by
    #1

    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&)'

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 11 Sept 2014, 09:46 last edited by
      #2

      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());@

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • S Offline
        S Offline
        srikanth.koorma
        wrote on 11 Sept 2014, 10:47 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 11 Sept 2014, 16:26 last edited by
          #4

          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;
          }@

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • S Offline
            S Offline
            srikanth.koorma
            wrote on 12 Sept 2014, 06:05 last edited by
            #5

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

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 12 Sept 2014, 07:44 last edited by
              #6

              You can put this question to SOLVED state.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              0

              1/6

              11 Sept 2014, 08:51

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved