Qt5 Tutorial QStringList - 2020
In this tutorial, we will learn about QStringList.
The QStringList class provides a list of strings.
QStringList inherits from QList<QString>. Like QList, QStringList is implicitly shared. It provides fast index-based access as well as fast insertions and removals. Passing string lists as value parameters is both fast and safe.
All of QList's functionality also applies to QStringList. For example, you can use isEmpty() to test whether the list is empty, and you can call functions like append(), prepend(), insert(), replace(), removeAll(), removeAt(), removeFirst(), removeLast(), and removeOne() to modify a QStringList. In addition, QStringList provides a few convenience functions that make handling lists of strings easier.
#include#include #include int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // A QString QString str = "A,B,C,D,E,F,G"; qDebug() << "QString str = " << str; QStringList List; qDebug() << "split str using ',' as a delimeter"; List = str.split(","); qDebug() << "List = " << List; foreach(QString item, List) qDebug() << "List items = " << item; qDebug() << "Replace one of the List item"; List.replaceInStrings("C","CCC"); qDebug() << "List = " << List; qDebug() << "Join all items in the List"; QString joinedString = List.join(", "); qDebug() << "joined string = " << joinedString; return a.exec(); }
Output:
QString str = "A,B,C,D,E,F,G" split str using ',' as a delimeter List = ("A", "B", "C", "D", "E", "F", "G") List items = "A" List items = "B" List items = "C" List items = "D" List items = "E" List items = "F" List items = "G" Replace one of the List item List = ("A", "B", "CCC", "D", "E", "F", "G") Join all items in the List joined string = "A, B, CCC, D, E, F, G"
To break up a string into a string list, we used the QString::split() function. The argument to split can be a single character, a string, or a QRegExp.
To concatenate all the strings in a string list into a single string (with an optional separator), we used the join() function.
The replaceInStrings() function calls QString::replace() on each string in the string list in turn.
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization