Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

How to Split QString for get values of each line



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



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


Log in to reply