QStringList Class

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:

 
Sélectionnez
    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:

 
Sélectionnez
    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:

 
Sélectionnez
    for (int i = 0; i &lt; fonts.size(); ++i)
         cout &lt;&lt; fonts.at(i).toLocal8Bit().constData() &lt;&lt; endl;

Java-style iterator:

 
Sélectionnez
    QStringListIterator javaStyleIterator(fonts);
    while (javaStyleIterator.hasNext())
         cout &lt;&lt; javaStyleIterator.next().toLocal8Bit().constData() &lt;&lt; endl;

STL-style iterator:

 
Sélectionnez
    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:

 
Sélectionnez
    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:

 
Sélectionnez
    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):

 
Sélectionnez
    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:

 
Sélectionnez
    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

Member Function Documentation

 

QStringList::QStringList()

Constructs an empty string list.

[default] QStringList::QStringList(const QString &str)

Constructs a string list that contains the given string,