How we can store string in array format?



  • 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?


  • Moderators

    @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?



  • @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.


  • Qt Champions 2018

    @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


Log in to reply
 

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