Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How we can store string in array format?
Forum Updated to NodeBB v4.3 + New Features

How we can store string in array format?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
4 Posts 3 Posters 4.0k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mohit Tripathi
    wrote on 29 Mar 2018, 06:02 last edited by Mohit Tripathi
    #1

    Suppose, we have
    char arr[2]
    arr[0]="abc" and arr[1]="mno" in C/C++.
    How we can do in Qt same thing? I have read about QByteArray but here is nothing about the position about 0 or 1.
    Could you please explain about it?

    QByteArray Q_tex_val,Q_tex_val1;
    QByteArray output1 = ui->lineEdit->text();
    int n=output1.size();
    int n1=output2.size();
    QByteArray value =output1.data()[n-1];
    QByteArray value1 =output2.data()[n1-1];
        for(int i=0;i<n;i++)
            {
                QByteArray value  =output1.data()[i];
                Q_tex_val=value.toUtf8();
                arduino->write(Q_tex_val.toStdString().c_str());
                arduino->flush();
                QThread::msleep(1000);
                arduino->waitForBytesWritten(100);
            }
    

    It is giving the error because the I am taking the data in form of string from Line text edit.
    Is there any way we can store the string in particular array?

    A 1 Reply Last reply 29 Mar 2018, 06:08
    0
    • M Mohit Tripathi
      29 Mar 2018, 06:02

      Suppose, we have
      char arr[2]
      arr[0]="abc" and arr[1]="mno" in C/C++.
      How we can do in Qt same thing? I have read about QByteArray but here is nothing about the position about 0 or 1.
      Could you please explain about it?

      QByteArray Q_tex_val,Q_tex_val1;
      QByteArray output1 = ui->lineEdit->text();
      int n=output1.size();
      int n1=output2.size();
      QByteArray value =output1.data()[n-1];
      QByteArray value1 =output2.data()[n1-1];
          for(int i=0;i<n;i++)
              {
                  QByteArray value  =output1.data()[i];
                  Q_tex_val=value.toUtf8();
                  arduino->write(Q_tex_val.toStdString().c_str());
                  arduino->flush();
                  QThread::msleep(1000);
                  arduino->waitForBytesWritten(100);
              }
      

      It is giving the error because the I am taking the data in form of string from Line text edit.
      Is there any way we can store the string in particular array?

      A Offline
      A Offline
      ambershark
      wrote on 29 Mar 2018, 06:08 last edited by ambershark
      #2

      @Mohit-Tripathi You'll have to be a bit clearer on your question. The c++ you listed would cause an access violation since you are overstepping your bounds on both arr[0] = "abc" and arr[1]="mno".

      Are you asking how to store an array of strings? If so you could use a QList<QString>. But again I'm not quite sure what the question is. Rephrase it or clarify a bit and I'm sure I or someone else could help.

      EDIT: I replied before your edit adding the code ... so what is the actual error you are getting?

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      M 1 Reply Last reply 29 Mar 2018, 06:12
      1
      • A ambershark
        29 Mar 2018, 06:08

        @Mohit-Tripathi You'll have to be a bit clearer on your question. The c++ you listed would cause an access violation since you are overstepping your bounds on both arr[0] = "abc" and arr[1]="mno".

        Are you asking how to store an array of strings? If so you could use a QList<QString>. But again I'm not quite sure what the question is. Rephrase it or clarify a bit and I'm sure I or someone else could help.

        EDIT: I replied before your edit adding the code ... so what is the actual error you are getting?

        M Offline
        M Offline
        Mohit Tripathi
        wrote on 29 Mar 2018, 06:12 last edited by
        #3

        @ambershark
        The error is conversion from 'QString' to non-scalar type 'QByteArray' requested.

        QByteArray output1 = ui->lineEdit->text();

        I know that I am getting the data in string format but I want to store this particular data into particular Array position.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 29 Mar 2018, 06:37 last edited by
          #4

          @Mohit-Tripathi said in How we can store string in array format?:

          @ambershark
          The error is conversion from 'QString' to non-scalar type 'QByteArray' requested.

          QByteArray output1 = ui->lineEdit->text();

          I know that I am getting the data in string format but I want to store this particular data into particular Array position.

          You should probably read up a bit about the differences between QString and QByteArray. QString is an 16-Bit Unicode string, while QByteArray is 8-bit, like char[].

          When you convert from QString to QByteArray, you need to specify the encoding your byte array should have afterwards. Depending on that, you can e.g. use QString::toLatin1() or QString::toUtf8(). If you need other encodings, you should look up QTextStream.

          Hope that clarifies it a bit.

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          3

          1/4

          29 Mar 2018, 06:02

          • Login

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