QStringList Class ReferenceThe QStringList class provides a list of strings. More... #include <QStringList> Inherits: QList<QString>. Note: All functions in this class are reentrant. Public Functions
Related Non-Members
Additional Inherited Members
Detailed DescriptionThe 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 stringsStrings can be added to a list using the append(), operator+=() and operator<<() functions. For example: QStringList fonts; fonts << "Arial" << "Helvetica" << "Times" << "Courier"; Iterating over the stringsTo 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 stringsQStringList 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" 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, 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): 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: 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
|
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.8 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com