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. How to split Array
QtWS25 Last Chance

How to split Array

Scheduled Pinned Locked Moved Unsolved General and Desktop
arrayc++11websocketstringintegers
6 Posts 3 Posters 4.6k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • ? A Former User

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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"))
      

      ?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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 ?

        J.HilkJ 1 Reply Last reply
        0
        • ? A Former User

          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 ?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

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

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

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

            J.HilkJ 1 Reply Last reply
            0
            • ? A Former User

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

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

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

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0

              • Login

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