convert QString list to QList<int>
-
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?
-
@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'.@SamurayH this
foreach(QString num, data3[0]){ std::cout <<num.toInt() << endl; }
return:
1
8
0
2
1
0
0
3
0no 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]); }
-
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?
-
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.
-
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.
@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 getASSERT failure in QList<T>::operator[]: "index out of range
-
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?
@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