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 QString for get values of each line
Forum Update on Tuesday, May 27th 2025

How to Split QString for get values of each line

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 1.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.
  • I Offline
    I Offline
    isan
    wrote on 22 Aug 2019, 16:14 last edited by
    #1

    I want to split below data by adding values 0 to 5 of each line to a double list like:

    QString data="#438,268,238,538,2019/8/20,16:24:47 #440,270,240,310,2019/8/20,16:24:48 #438,268,238,538,2019/8/20,16:24:49 #440,270,240,540,2019/8/20,16:24:50 #440,270,240,540,2019/8/20,16:24:51 #440,270,240,540,2019/8/20,16:24:52
    ....."
    

    to:

    QList<double>row0,row1,...,row5;
    row0<<438<<440<<438<<440,...
    row1<<268<<270<<268,....
    .
    .
    .
    
    row5<<16:24:47<<16:24:48<<16:24:49,.....
    

    I try this way

    QList<double>val1;
     QStringList line=getRecord.split("#");
    
        for(int u=0;u<line.size();u++)        
      val1.append(line.at(0).toDouble());
        qDebug()<<"val1"<<val1;
    

    But it only gives "4",
    how can I do this?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on 22 Aug 2019, 17:03 last edited by mpergand
      #2
      QString data="#438,268,238,538,2019/8/20,16:24:47 "
                   "#440,270,240,310,2019/8/20,16:24:48 "
                   "#438,268,238,538,2019/8/20,16:24:49 "
                   "#440,270,240,540,2019/8/20,16:24:50 "
                   "#440,270,240,540,2019/8/20,16:24:51 "
                   "#440,270,240,540,2019/8/20,16:24:52";
      
      QList<double> rows[4];
      
      int main(int argc, char *argv[])
      {
      
          QStringList records=data.split('#',QString::SkipEmptyParts);
      
          for(const QString& record : records)
              {
              QStringList data=record.split(',');
              //qDebug()<<data;
      
              if(data.length()!=6)  
                  {
                  //qDebug()<<"Error"<<data;
                  continue;
                  }
      
              for(int i=0; i<4; i++)
                  {
                  //qDebug()<<data.at(i).toDouble();
                  rows[i]<<data.at(i).toDouble();
                  }
      
              }
          //qDebug()<<records;
          qDebug()<<rows[0];
          qDebug()<<rows[1];
          qDebug()<<rows[2];
          qDebug()<<rows[3];
      
          return 0;
      }
      

      Rows 5 & 6 are not double values.
      You can convert these to QDate of course (look at the doc for more info)

      1 Reply Last reply
      1

      1/2

      22 Aug 2019, 16:14

      • Login

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