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. convert QString list to QList<int>
QtWS25 Last Chance

convert QString list to QList<int>

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 7.4k 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.
  • I Offline
    I Offline
    isan
    wrote on last edited by isan
    #1

    I have a QString List of time like timelist[0]="18:21:03";
    I want to convert it to QList< int> I use this:

    QStringList timelist;
    QList<int> val3;
      for (int v = 0; v <= 300 ; v++) {
     foreach(QString num, timelist[v]){
       std::cout << "\n"<<num.toInt() << endl; 
        val3.append(num.toInt());
    }
      std:: cout<<"\n out"<<val3[v];
     }
    
     but  I get **ASSERT failure in QList<T>::operator[]: "index out of range"**
    

    how should I define QList?

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • SamurayHS Offline
      SamurayHS Offline
      SamurayH
      wrote on last edited by
      #2

      @isan are you trying to convert the time to seconds or whatever? Because QString("18:21:03").toInt(); will return 0.
      Can you clarify what is ' converting a "18:21:03" to int'.

      "قال رسول الله صلى الله عليه وسلم : " أحب الناس إلى الله أنفعهم للناس

      I 1 Reply Last reply
      0
      • SamurayHS SamurayH

        @isan are you trying to convert the time to seconds or whatever? Because QString("18:21:03").toInt(); will return 0.
        Can you clarify what is ' converting a "18:21:03" to int'.

        I Offline
        I Offline
        isan
        wrote on last edited by isan
        #3

        @SamurayH this

        foreach(QString num, data3[0]){
                std::cout <<num.toInt() << endl;  
          }
        

        return:

        1
        8
        0
        2
        1
        0
        0
        3
        0

        no I don't want to convert time to second , I want to convert qstringlist of time(read from excel file) to qlist<int> :

        QLineSeries *series = new QLineSeries();
            for(int y=0 ; y<val3.size() ; y++){
                series->append(val3[y],val1[y]);
            }
        
        1 Reply Last reply
        0
        • I isan

          I have a QString List of time like timelist[0]="18:21:03";
          I want to convert it to QList< int> I use this:

          QStringList timelist;
          QList<int> val3;
            for (int v = 0; v <= 300 ; v++) {
           foreach(QString num, timelist[v]){
             std::cout << "\n"<<num.toInt() << endl; 
              val3.append(num.toInt());
          }
            std:: cout<<"\n out"<<val3[v];
           }
          
           but  I get **ASSERT failure in QList<T>::operator[]: "index out of range"**
          

          how should I define QList?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @isan

          QStringList timelist;
            for (int v = 0; v <= 300 ; v++) {
           foreach(QString num, timelist[v]){
          
           but  I get **ASSERT failure in QList<T>::operator[]: "index out of range"**
          

          Who knows whether there are 301 strings in timelist[]? We certainly don't.

          1 Reply Last reply
          3
          • SamurayHS Offline
            SamurayHS Offline
            SamurayH
            wrote on last edited by SamurayH
            #5

            Try this:

            QList<int> time2Int(const QStringList& timeList)
            {
               QList<int> intList;
               foreach(const QString& time, timeList)
                 for(int i = 0; i < time.size(); i++)
                  {
                    bool isNum = false; // boolean to check whether the 'time.at(i)' is a number
                    int n = QString(time.at(i)).toInt(&isNum); // your int
            
                    if(isNum) 
                        intList << n;
                  }
            
              return intList;
            }
            

            If you want a QList<int> foreach time, the function above will return then a QList<QList<int>>, which is a list of 'int list' :

            QList<QList<int>> time2Int(const QStringList& timeList)
            {
               QList<QList<int>> gIntList; // Global list
               foreach(const QString& time, timeList)
               {
                 QList<int> intList;
                 for(int i = 0; i < time.size(); i++)
                  {
                    bool isNum = false; // boolean to check whether the 'time.at(i)' is a number
                    int n = QString(time.at(i)).toInt(&isNum); // your int
            
                    if(isNum)
                        intList << n;
                  }
            
                 gIntList << intList;
               }
            
              return gIntList;
            }
            

            I hope that this is what you're looking for.

            "قال رسول الله صلى الله عليه وسلم : " أحب الناس إلى الله أنفعهم للناس

            I 1 Reply Last reply
            2
            • SamurayHS SamurayH

              Try this:

              QList<int> time2Int(const QStringList& timeList)
              {
                 QList<int> intList;
                 foreach(const QString& time, timeList)
                   for(int i = 0; i < time.size(); i++)
                    {
                      bool isNum = false; // boolean to check whether the 'time.at(i)' is a number
                      int n = QString(time.at(i)).toInt(&isNum); // your int
              
                      if(isNum) 
                          intList << n;
                    }
              
                return intList;
              }
              

              If you want a QList<int> foreach time, the function above will return then a QList<QList<int>>, which is a list of 'int list' :

              QList<QList<int>> time2Int(const QStringList& timeList)
              {
                 QList<QList<int>> gIntList; // Global list
                 foreach(const QString& time, timeList)
                 {
                   QList<int> intList;
                   for(int i = 0; i < time.size(); i++)
                    {
                      bool isNum = false; // boolean to check whether the 'time.at(i)' is a number
                      int n = QString(time.at(i)).toInt(&isNum); // your int
              
                      if(isNum)
                          intList << n;
                    }
              
                   gIntList << intList;
                 }
              
                return gIntList;
              }
              

              I hope that this is what you're looking for.

              I Offline
              I Offline
              isan
              wrote on last edited by isan
              #6

              @SamurayH said in convert QString list to QList<int>:

              QList<QList<int>> time2Int(const QStringList& timeList)

              thank you
              QList<QList<int>> time2Int(const QStringList& timeList) returns the (1,8,2,1,0,3)
              stringlist can not be converted to the same type (18:21:03)?
              I need string list to be converted to the same type(18:21:03 )
              and still I get

              ASSERT failure in QList<T>::operator[]: "index out of range
              
              1 Reply Last reply
              0
              • I isan

                I have a QString List of time like timelist[0]="18:21:03";
                I want to convert it to QList< int> I use this:

                QStringList timelist;
                QList<int> val3;
                  for (int v = 0; v <= 300 ; v++) {
                 foreach(QString num, timelist[v]){
                   std::cout << "\n"<<num.toInt() << endl; 
                    val3.append(num.toInt());
                }
                  std:: cout<<"\n out"<<val3[v];
                 }
                
                 but  I get **ASSERT failure in QList<T>::operator[]: "index out of range"**
                

                how should I define QList?

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

                @isan said in convert QString list to QList<int>:

                QStringList timelist;
                QList<int> val3;
                for (int v = 0; v <= 300 ; v++) {
                foreach(QString num, timelist[v]){
                std::cout << "\n"<<num.toInt() << endl;
                val3.append(num.toInt());
                }
                std:: cout<<"\n out"<<val3[v];
                }

                     but  I get **ASSERT failure in QList<T>::operator[]: "index out of range"**
                how should I define QList?
                

                You have the potential to Go out of bounds at at least 2 positions.
                Like @JonB said, we don‘t know if 300 is inside the Range of v.

                It doesn‘t really matter anyway, because this line after the loop

                std:: cout<<"\n out"<<val3[v];

                Is garantied to always be out of range.

                Val has 300 entries, and you‘re trying to access the 301‘s


                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
                3

                • Login

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