QStringList Class
The QStringList class provides a list of strings. More...
Header: | #include <QStringList> |
qmake: | QT += core |
Note: All functions in this class are reentrant.
Public Functions
QStringList() | |
bool | contains(int str, int cs = ...) const |
QStringList | filter(const QString &str, int cs = ...) const |
QStringList | filter(const int &rx) const |
int | indexOf(const int &rx, int from = 0) const |
int | indexOf(int &rx, int from = 0) const |
QString | join(const QString &sep) const |
QString | join(int sep) const |
int | lastIndexOf(const int &rx, int from = -1) const |
int | lastIndexOf(int &rx, int from = -1) const |
int | removeDuplicates() |
QStringList & | replaceInStrings(const QString &before, const QString &after, int cs = ...) |
QStringList & | replaceInStrings(const int &rx, const QString &after) |
void | sort(int cs = ...) |
QStringList | operator+(const QStringList &other) const |
QStringList & | operator<<(const QString &str) |
QStringList & | operator<<(const QStringList &l) |
QStringList & | operator<<(const QList<QString> &l) |
Related Non-Members
typedef | QMutableStringListIterator |
typedef | QStringListIterator |
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:
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 << "Courier" << "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 < fonts.size(); ++i) cout << fonts.at(i).toLocal8Bit().constData() << endl;
Java-style iterator:
QStringListIterator javaStyleIterator(fonts); while (javaStyleIterator.hasNext()) cout << javaStyleIterator.next().toLocal8Bit().constData() << endl;
STL-style iterator:
QStringList::const_iterator constIterator; for (constIterator = fonts.constBegin(); constIterator != fonts.constEnd(); ++constIterator) 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:
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 << "$QTDIR/src/moc/moc.y" << "$QTDIR/src/moc/moc.l" << "$QTDIR/include/qconfig.h"; files.replaceInStrings("$QTDIR", "/usr/lib/qt"); // files: [ "/usr/lib/qt/src/moc/moc.y", ...]
See also QString.
Member Function Documentation
QStringList::QStringList()
Default constructs an instance of QStringList.
bool QStringList::contains(int str, int cs = ...) const
QStringList QStringList::filter(const QString &str, int cs = ...) const
QStringList QStringList::filter(const int &rx) const
int QStringList::indexOf(const int &rx, int from = 0) const
int QStringList::indexOf(int &rx, int from = 0) const
QString QStringList::join(const QString &sep) const
QString QStringList::join(int sep) const
int QStringList::lastIndexOf(const int &rx, int from = -1) const
int QStringList::lastIndexOf(int &rx, int from = -1) const
int QStringList::removeDuplicates()
QStringList &QStringList::replaceInStrings(const QString &before, const QString &after, int cs = ...)
QStringList &QStringList::replaceInStrings(const int &rx, const QString &after)
void QStringList::sort(int cs = ...)
QStringList QStringList::operator+(const QStringList &other) const
QStringList &QStringList::operator<<(const QString &str)
QStringList &QStringList::operator<<(const QStringList &l)
QStringList &QStringList::operator<<(const QList<QString> &l)
Related Non-Members
typedef QMutableStringListIterator
The QStringListIterator type definition provides a Java-style non-const iterator for QStringList.
QStringList provides both Java-style iterators and STL-style iterators. The Java-style non-const iterator is simply a type definition for QMutableListIterator<QString>.
See also QStringListIterator and QStringList::iterator.
typedef QStringListIterator
The QStringListIterator type definition provides a Java-style const iterator for QStringList.
QStringList provides both Java-style iterators and STL-style iterators. The Java-style const iterator is simply a type definition for QListIterator<QString>.
See also QMutableStringListIterator and QStringList::const_iterator.