How to split Array



  • I am new for Qt C++. I am trying to receive a string from server which includes data.

    I have tried with one String sending from server, received via websocket. I have stored that, then taken to another thread. Here I have posted my code, what I have done. I don't know whether that is correct or not.

    Kindly assist me to get a solution for that.

    websocket.cpp (I will receive the string from server, it contains data)

    else if(message.contains("starting")) 
    {
       startvalue = message;
       qDebug() << startvalue;
    }
    

    Received message from server

    "start|value|10|5"
    
    startval = EchoClient::startvalue;
    QStringList value = startval.split("|");
    

    So, I can easily split and assign the function.

    For Example if I receive like this

    "start|value|1|5" , "start|value|3|5" , "start|value|5|5" , "start|value|7|5" , "start|value|10|6"
    

    I can split but how can I complete each task one by one ?

    Task 1 : start|value|1|5
    Task 2 : start|value|3|5
    Task 3 : start|value|5|5
    Task 4 : start|value|7|5
    Task 5 : start|value|10|6
    

    How can I use Array for this ? First I want to split each one then One by one I want to complete all task, it should not get interfere.


  • Moderators

    @Geeva Sorry, but I don't get what exactly the problem is?
    "I can split but how can I complete each task one by one ?" - what do you mean? What is the problem?
    Also shouldn't this be

    else if(message.contains("starting"))
    

    rather

    else if(message.contains("start"))
    

    ?



  • Ya I have wrongly mentioned. It will be start only. What I have done.

    This message I have received from server

    "start|value|10|5"
    

    Received message spitted using this

    QStringList value = startval.split("|");
    
    x = 10;
    y = 5;
    

    Now I can do whatever I want. I have tried that is working fine.

    Here is the problem. If I receive like this

    "start|value|1|5" , "start|value|3|5" , "start|value|5|5" , "start|value|7|5" , "start|value|10|6"
    

    Everything have different task, lets say above 5 tasks

    Task 1 : start|value|1|5
    Task 2 : start|value|3|5
    Task 3 : start|value|5|5
    Task 4 : start|value|7|5
    Task 5 : start|value|10|6
    

    Question 1 : I think I can able to split similar I have tried with one task. Or else any other ways I want to use ?

    Question 2 : After splitting now I have 5 tasks. I want to complete one by one. After completed first task then only I can use and complete 2nd task. How can I create those structure ? How can I use Array for that ? or else any other way ?



  • @Geeva array is a bit inflexible for your task here, as they, generally, have a fixed size.

    I would suggest using a QList or an QVector

    QList<QList<QString> > myList;
    //or QVector<QVector<QString> > myVector
    

    with Append you can add an other List. In your case here:

    QList<QList<QString>> myList;
    
    myList.append(startval.split("|"));
    

    You access the list via the first indx of the list:

    QStringList sl = myList.at(0);
    QString s = myList.at(0).at(0);
    //alternativly myList[0][0]
    


  • @J-Hilk Thanks for your response. I have tried QList for one task that is working fine, but how can I check with remaining task ?

    It should complete the first task then only it should go for another task.

    What I have done with first task.

    startvalue = Server::Start;
    qDebug() << "Value Received from server :" << startvalue;
    QStringList first = startvalue.split("|");
    int firstSize = first.size();
    qDebug() << "size :" << firstSize;
    
    if(firstSize == 5){
         first1 = first[2];
         first2 = first[3];
      }..........(continue)
    

    From here I will check array size and split after that whatever the value I want I will take and use this is for one task.

    For multiple task ? I don't have experience, kindly elaborate your answer.



  • @Geeva
    The easies way is to move your task into its own function that expects q QList<QString> as an argument:

    void myClass::myFunction(QList<QString> startvalue){
        //do stuff eg:
        qDebug() << "Value Received from server :" << startvalue;
        QStringList first = startvalue.split("|");
        int firstSize = first.size();
        qDebug() << "size :" << firstSize;
    
        if(firstSize == 5){
         first1 = first[2];
         first2 = first[3];
      }..........(continue)
    }
    

    then you call that function for each and every item in your QList<QList<QString>>

    QList<QList<QString> > myList
    for(QList<QString> ls : myList){
        myFunction(ls);
    }
    


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