QStringList Class Reference

The QStringList class provides a list of strings.

  1. #include <QStringList>

Inherits: QList<QString>.

Note: All functions in this class are reentrant.

Detailed Description

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:

Adding strings

Strings can be added to a list using the append(), operator+=() and operator<<() functions. For example:

  1.     QStringList fonts;
  2.     fonts << "Arial" << "Helvetica" << "Times" << "Courier";

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:

  1.     for (int i = 0; i < fonts.size(); ++i)
  2.          cout << fonts.at(i).toLocal8Bit().constData() << endl;

Java-style iterator:

  1.     QStringListIterator javaStyleIterator(fonts);
  2.     while (javaStyleIterator.hasNext())
  3.          cout << javaStyleIterator.next().toLocal8Bit().constData() << endl;

STL-style iterator:

  1.     QStringList::const_iterator constIterator;
  2.     for (constIterator = fonts.constBegin(); constIterator != fonts.constEnd();
  3.            ++constIterator)
  4.         cout << (*constIterator).toLocal8Bit().constData() << 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:

  1.     QString str = fonts.join(",");
  2.      // str == "Arial,Helvetica,Times,Courier"

To break up a string into a string list, use the QString::split() function:

  1.     QStringList list;
  2.     list = str.split(",");
  3.      // list: ["Arial", "Helvetica", "Times", "Courier"]

The argument to split can be a single character, a string, or a 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):

  1.     QStringList monospacedFonts = fonts.filter(QRegExp("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:

  1.     QStringList files;
  2.     files << "$QTDIR/src/moc/moc.y"
  3.           << "$QTDIR/src/moc/moc.l"
  4.           << "$QTDIR/include/qconfig.h";
  5.  
  6.     files.replaceInStrings("$QTDIR", "/usr/lib/qt");
  7.     // files: [ "/usr/lib/qt/src/moc/moc.y", ...]

See also QString.

Public Functions

Toggle details QStringList

QStringListQStringList () ()

Constructs an empty string list.

Look up this member in the source code.

Toggle details QStringList

QStringListQStringList ( const QString &str ) ( const QString &str )

Constructs a string list that contains the given string, str. Longer lists are easily created like this:

  1.     QStringList longerList = (QStringList() << str1 << str2 << str3);

See also append().

Look up this member in the source code.

Toggle details QStringList

QStringListQStringList ( const QStringList &other ) ( const QStringList &other )

Constructs a copy of the other string list.

This operation takes constant time because QStringList is implicitly shared, making the process of returning a QStringList from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time.

See also operator=().

Look up this member in the source code.

Toggle details QStringList

QStringListQStringList ( const QList <QString > &other ) ( const QList <QString > &other )

Constructs a copy of other.

This operation takes constant time, because QStringList is implicitly shared. This makes returning a QStringList from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time.

See also operator=().

Look up this member in the source code.

Toggle details bool QStringList

containscontains ( const QString &str , Qt::CaseSensitivity cs=Qt::CaseSensitive ...) ( const QString &str , Qt::CaseSensitivity cs=Qt::CaseSensitive )const

Returns true if the list contains the string str; otherwise returns false. The search is case insensitive if cs is Qt::CaseInsensitive; the search is case sensitive by default.

See also indexOf(), lastIndexOf(), and QString::contains().

Look up this member in the source code.

Toggle details QStringList QStringList

filterfilter ( const QString &str , Qt::CaseSensitivity cs=Qt::CaseSensitive ...) ( const QString &str , Qt::CaseSensitivity cs=Qt::CaseSensitive )const

Returns a list of all the strings containing the substring str.

If cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.

  1.     QStringList list;
  2.     list << "Bill Murray" << "John Doe" << "Bill Clinton";
  3.  
  4.     QStringList result;
  5.     result = list.filter("Bill");
  6.     // result: ["Bill Murray", "Bill Clinton"]

This is equivalent to

  1.     QStringList result;
  2.     foreach (const QString &str, list)  {
  3.         if (str.contains("Bill"))
  4.             result += str;
  5.     }

See also contains().

Look up this member in the source code.

Toggle details QStringList QStringList

filterfilter ( const QRegExp &rx ) ( const QRegExp &rx )const

This is an overloaded function.

Returns a list of all the strings that match the regular expression rx.

Look up this member in the source code.

Toggle details int QStringList

indexOfindexOf ( const QRegExp &rx , int from=0 ) ( const QRegExp &rx , int from=0 )const

Returns the index position of the first exact match of rx in the list, searching forward from index position from. Returns -1 if no item matched.

By default, this function is case sensitive.

See also lastIndexOf(), contains(), and QRegExp::exactMatch().

Look up this member in the source code.

Toggle details int QStringList

indexOfindexOf ( const QString &value , int from=0 ) ( const QString &value , int from=0 )const

Returns the index position of the first occurrence of value in the list, searching forward from index position from. Returns -1 if no item matched.

See also lastIndexOf(), contains(), and QList::indexOf().

Look up this member in the source code.

Toggle details int QStringList

indexOfindexOf ( QRegExp &rx , int from=0 ) ( QRegExp &rx , int from=0 )const

This function overloads indexOf().

Returns the index position of the first exact match of rx in the list, searching forward from index position from. Returns -1 if no item matched.

By default, this function is case sensitive.

If an item matched, the rx regular expression will contain the matched objects (see QRegExp::matchedLength, QRegExp::cap).

See also lastIndexOf(), contains(), and QRegExp::exactMatch().

Look up this member in the source code.

Toggle details QString QStringList

joinjoin ( const QString &separator ) ( const QString &separator )const

Joins all the string list's strings into a single string with each element separated by the given separator (which can be an empty string).

See also QString::split().

Look up this member in the source code.

Toggle details int QStringList

lastIndexOflastIndexOf ( const QRegExp &rx , int from=-1 ) ( const QRegExp &rx , int from=-1 )const

Returns the index position of the last exact match of rx in the list, searching backward from index position from. If from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

By default, this function is case sensitive.

See also indexOf(), contains(), and QRegExp::exactMatch().

Look up this member in the source code.

Toggle details int QStringList

lastIndexOflastIndexOf ( const QString &value , int from=-1 ...) ( const QString &value , int from=-1 )const

Returns the index position of the last occurrence of value in the list, searching backward from index position from. If from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

By default, this function is case sensitive.

See also indexOf() and QList::lastIndexOf().

Look up this member in the source code.

Toggle details int QStringList

lastIndexOflastIndexOf ( QRegExp &rx , int from=-1 ) ( QRegExp &rx , int from=-1 )const

This function overloads lastIndexOf().

Returns the index position of the last exact match of rx in the list, searching backward from index position from. If from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

By default, this function is case sensitive.

If an item matched, the rx regular expression will contain the matched objects (see QRegExp::matchedLength, QRegExp::cap).

See also indexOf(), contains(), and QRegExp::exactMatch().

Look up this member in the source code.

Toggle details int QStringList

removeDuplicatesremoveDuplicates () ()

This function removes duplicate entries from a list. The entries do not have to be sorted. They will retain their original order.

Returns the number of removed entries.

Look up this member in the source code.

Toggle details QStringList & QStringList

replaceInStringsreplaceInStrings ( const QString &before , const QString &after , Qt::CaseSensitivity cs=Qt::CaseSensitive ...) ( const QString &before , const QString &after , Qt::CaseSensitivity cs=Qt::CaseSensitive )

Returns a string list where every string has had the before text replaced with the after text wherever the before text is found. The before text is matched case-sensitively or not depending on the cs flag.

For example:

  1.     QStringList list;
  2.     list << "alpha" << "beta" << "gamma" << "epsilon";
  3.     list.replaceInStrings("a", "o");
  4.     // list == ["olpho", "beto", "gommo", "epsilon"]

See also QString::replace().

Look up this member in the source code.

Toggle details QStringList & QStringList

replaceInStringsreplaceInStrings ( const QRegExp &rx , const QString &after ...) ( const QRegExp &rx , const QString &after )

This is an overloaded function.

Replaces every occurrence of the regexp rx, in each of the string lists's strings, with after. Returns a reference to the string list.

For example:

  1.     QStringList list;
  2.     list << "alpha" << "beta" << "gamma" << "epsilon";
  3.     list.replaceInStrings(QRegExp("^a"), "o");
  4.     // list == ["olpha", "beta", "gamma", "epsilon"]

For regular expressions that contain capturing parentheses, occurrences of \1, \2, ..., in after are replaced with rx.cap(1), rx.cap(2), ...

For example:

  1.     QStringList list;
  2.     list << "Bill Clinton" << "Murray, Bill";
  3.     list.replaceInStrings(QRegExp("^(.*), (.*)$"), "\\2 \\1");
  4.     // list == ["Bill Clinton", "Bill Murray"]

Look up this member in the source code.

Toggle details void QStringList

sortsort () ()

Sorts the list of strings in ascending order (case sensitively).

Sorting is performed using Qt's qSort() algorithm, which operates in linear-logarithmic time, i.e. O(n log n).

If you want to sort your strings in an arbitrary order, consider using the QMap class. For example, you could use a QMap<QString, QString> to create a case-insensitive ordering (e.g. with the keys being lower-case versions of the strings, and the values being the strings), or a QMap<int, QString> to sort the strings by some integer index.

See also qSort().

Look up this member in the source code.

Toggle details QStringList QStringList

operator+operator+ ( const QStringList &other ) ( const QStringList &other )const

Returns a string list that is the concatenation of this string list with the other string list.

See also append().

Look up this member in the source code.

Toggle details QStringList & QStringList

operator<<operator<< ( const QString &str ) ( const QString &str )

Appends the given string, str, to this string list and returns a reference to the string list.

See also append().

Look up this member in the source code.

Toggle details QStringList & QStringList

operator<<operator<< ( const QStringList &other ( const QStringList &other )

This is an overloaded function.

Appends the other string list to the string list and returns a reference to the latter string list.

Look up this member in the source code.

Toggle details Iterator QStringList

fromLastfromLast () () Qt3-support

Use end() instead.

For example, if you have code like

  1. QStringList::Iterator i = list.fromLast();

you can rewrite it as

  1.                     QStringList::Iterator i = list.isEmpty() ? list.end() : --list.end();

Look up this member in the source code.

Toggle details ConstIterator QStringList

fromLastfromLast () ()const Qt3-support

Use end() instead.

For example, if you have code like

  1. QStringList::ConstIterator i = list.fromLast();

you can rewrite it as

  1.                     QStringList::ConstIterator i = list.isEmpty() ? list.end() : --list.end();

Look up this member in the source code.

Toggle details QStringList QStringList

grepgrep ( const QString &str , bool cs=true ...) ( const QString &str , bool cs=true )const Qt3-support

Use filter() instead.

Look up this member in the source code.

Toggle details QStringList QStringList

grepgrep ( const QRegExp &rx ) ( const QRegExp &rx )const Qt3-support

Use filter() instead.

Look up this member in the source code.

Toggle details QStringList & QStringList

gresgres ( const QRegExp &rx , const QString &after ...) ( const QRegExp &rx , const QString &after ) Qt3-support

Use replaceInStrings() instead.

Look up this member in the source code.

Toggle details QStringList & QStringList

gresgres ( const QString &before , const QString &after , bool cs=true ...) ( const QString &before , const QString &after , bool cs=true ) Qt3-support

Use replaceInStrings() instead.

Look up this member in the source code.

Toggle details QStringList QStringList

splitsplit ( const QRegExp &sep , const QString &str , bool allowEmptyEntries=false ...) ( const QRegExp &sep , const QString &str , bool allowEmptyEntries=false ) [static] Qt3-support

Use QString::split(sep, QString::SkipEmptyParts) or QString::split(sep, QString::KeepEmptyParts) instead.

Be aware that the QString::split()'s return value is a QStringList that always contains at least one element, even if str is empty.

See also join() and QString::section().

Look up this member in the source code.

Toggle details QStringList QStringList

splitsplit ( const QChar &sep , const QString &str , bool allowEmptyEntries=false ...) ( const QChar &sep , const QString &str , bool allowEmptyEntries=false ) [static] Qt3-support

This is an overloaded function.

This version of the function uses a QChar as separator.

See also join() and QString::section().

Look up this member in the source code.

Toggle details QStringList QStringList

splitsplit ( const QString &sep , const QString &str , bool allowEmptyEntries=false ...) ( const QString &sep , const QString &str , bool allowEmptyEntries=false ) [static] Qt3-support

This is an overloaded function.

This version of the function uses a QString as separator.

See also join() and QString::section().

Look up this member in the source code.

Notes provided by the Qt Community
Informative
  • 0

Votes: 0

Coverage: Qt library 4.7

Picture of Diluka Diluka

Hobby Entomologist
23 notes

How to reverse a QList?

I found that method in one of the web site hope it will help some one to implement their applications

  1. template <typename T>
  2. QList<T> reversed( const QList<T> & in ) {
  3.     QList<T> result;
  4.     result.reserve( in.size() ); // reserve is new in Qt 4.7
  5.     std::reverse_copy( in.begin(), in.end(), std::back_inserter( result ) );
  6.     return result;
  7. }

[Revisions]