QStringList Class▲
-
Header: QStringList
-
qmake: QT += core
-
Group: QStringList is part of tools, Implicitly Shared Classes, string-processing
Detailed Description▲
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:
Initializing▲
The default constructor creates an empty list. You can use the initializer-list constructor to create a list with elements:
QStringList fonts =
{
"Arial"
, "Helvetica"
, "Times"
}
;
Adding Strings▲
Strings can be added to a list using the insert() append(), operator+=() and operator<<() functions.
operator<<() can be used to conveniently add multiple elements to a list:
fonts &
lt;&
lt; "Courier"
&
lt;&
lt; "Verdana"
;
Iterating Over the Strings▲
To iterate over a list, you can either use index positions or QList's Java-style and STL-style iterator types:
Indexing:
for
(int
i =
0
; i &
lt; fonts.size(); ++
i)
cout &
lt;&
lt; fonts.at(i).toLocal8Bit().constData() &
lt;&
lt; endl;
Java-style iterator:
QStringListIterator javaStyleIterator(fonts);
while
(javaStyleIterator.hasNext())
cout &
lt;&
lt; javaStyleIterator.next().toLocal8Bit().constData() &
lt;&
lt; endl;
STL-style iterator:
QStringList::
const_iterator constIterator;
for
(constIterator =
fonts.constBegin(); constIterator !=
fonts.constEnd();
++
constIterator)
cout &
lt;&
lt; (*
constIterator).toLocal8Bit().constData() &
lt;&
lt; endl;
The QStringListIterator class is simply a type definition for QListIterator<QString>. QStringList also provide the QMutableStringListIterator class which is a type definition for QMutableListIterator<QString>.
Manipulating the Strings▲
QStringList provides several functions allowing you to manipulate the contents of a list. You can concatenate all the strings in a string list into a single string (with an optional separator) using the join() function. For example:
QString str =
fonts.join(", "
);
// str == "Arial, Helvetica, Times, Courier"
The argument to join can be a single character or a string.
To break up a string into a string list, use the QString::split() function:
QStringList list;
list =
str.split(','
);
// list: ["Arial", "Helvetica", "Times", "Courier"]
The argument to split can be a single character, a string, a QRegularExpression or a (deprecated) QRegExp.
In addition, the operator+() function allows you to concatenate two string lists into one. To sort a string list, use the sort() function.
QString list also provides the filter() function which lets you to extract a new list which contains only those strings which contain a particular substring (or match a particular regular expression):
QStringList monospacedFonts =
fonts.filter(QRegularExpression("Courier|Fixed"
));
The contains() function tells you whether the list contains a given string, while the indexOf() function returns the index of the first occurrence of the given string. The lastIndexOf() function on the other hand, returns the index of the last occurrence of the string.
Finally, the replaceInStrings() function calls QString::replace() on each string in the string list in turn. For example:
QStringList files;
files &
lt;&
lt; "$QTDIR/src/moc/moc.y"
&
lt;&
lt; "$QTDIR/src/moc/moc.l"
&
lt;&
lt; "$QTDIR/include/qconfig.h"
;
files.replaceInStrings("$QTDIR"
, "/usr/lib/qt"
);
// files: [ "/usr/lib/qt/src/moc/moc.y", ...]
See Also▲
See also QString