Converting qstring into qlist
-
Hello.
Is there any way thatI can conver a qstring into a qlist.
Here is a example:
string: "1 4 2 6 4"
list: 1 4 2 6 4I am trying to do this because I want to make a simple program GUI that will recive a array wth a qlineedit, then it will do the sorting, and the it will display a sorted list.
Here is a example:
input: 1 5 2 4 3
output: 1 2 3 4 5 -
Try "QString::split":http://developer.qt.nokia.com/doc/qt-4.8/qstring.html#split function. It convert to QStringList but I think it should be enough
-
You just need to "split":http://developer.qt.nokia.com/doc/qt-4.8/qstring.html#split the QString. The result is a list of strings.
EDIT : oops, post is delayed. Anyway, to sort, you need QList of Int and use "qSort":http://developer.qt.nokia.com/doc/qt-4.8/qtalgorithms.html#qSort
-
This works, but please use some error checking:
@QList<int> list;
QString input;
QStringList slist;
slist = input.split(" ");
bool allOk(true);
bool ok;
for (int x = 0; x <= slist.count()-1 && allOk; x++) {
list.append(slist.at(x).toInt(&ok));
allOk &= ok;
}if (!allOk) {
// handle the problem
}
@ -
Thank you very much for your help.
-
Just another example, this time taking control over more than one space between entries.
@QString inputString = "1 3 5 6 2 4";
QStringList listProcessing;
QList<int> sortedList;
bool conversionState = true;listProcessing = inputString.split(" ", QString::SkipEmptyParts);
listProcessing.sort();for(int index = 0; index <= (listProcessing.size() - 1); ++index) {
sortedList.append(listProcessing.at(index).toInt(&conversionState)); if(!conversionState) break;
}// for
if(!conversionState) {
// Handling the conversion error. ...
}// if@