How to use QStringList in Qt
Article Metadata
Tested with
Devices(s): Symbian emulator
Compatibility
Platform(s): Qt
Article
Keywords: QStringList
Created: james1980
(17 Jan 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
This article demonstrates how to use QStringList. QStringList inherits from QList<QString>. Like QList, QStringListis 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.
Various Function
- To make a String List.
QStringList name,namesplit; name << "james 1980" << "james bond" << "paul" << "jonny joker";
- To join all the string in string list.
str1 = name.join(",");//str1="james 1980,james bond,paul,jonny joker"
- To break up a string into a string list.
namesplit = str1.split(",");// namesplit is same as name
- Extract a new list which contains only those strings which contain a particular substring (or match a particular regular expression).
namesplit = name.filter("james");//namesplit = ["james 1980","james bond"]
- To replace String.
namesplit.replaceInStrings("a", "o");
Code Snippet
#include <QApplication>
#include <QString>
#include <QStringList>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QString str = "world";
QString str1;
QStringList name,namesplit;
name << "james 1980" << "james bond" << "paul" << "jonny joker";
str1 = name.join(",");//str1="james 1980,james bond,paul,jonny joker"
namesplit = str1.split(",");// namesplit is same as name
namesplit = name.filter("james");//namesplit = ["james1980","james bond"]
bool a = name.contains(str);//Returns true if the list contains the string str; otherwise returns false.
namesplit.replaceInStrings("a", "o");// Replace all the occurrence of "a" with "o"
name.sort();//Sorts the list of strings in ascending order (case sensitively).
return app.exec();
}
See also
Documentation for QStringList
Originally by James1980, updates by Mind Freak


Article shows the various function of QstringList and appling method for that. It also shows the files which are included in the source code (Code snippet). In this article there is no need of any extra thing like more explanation or image. $ ---fasttrack 19:39, 11 September 2009 (UTC)
There's nothing Symbian-specific about QStringList, edited the article to be more generic $ ---treinio , 04 Feb 2010