QStringList Class Reference

[ QtCore module]

QStringList 类提供字符串列表。 更多...

方法

Special Methods


详细描述

A Python list of Python string or unicode objects or QString instances may be used whenever a QStringList is expected.

QStringList 类提供字符串列表。

QStringList inherits from QList < QString >. 像 QList , QStringList is 隐式共享 . 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 (),和 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+= () 和 operator<< () 函数。 例如:

     QStringList fonts;
     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:

     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;
			

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"
			

To break up a string into a string list, use the QString.split () 函数:

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

The argument to split can be a single character, a string, or a QRegExp .

此外, operator+ () function allows you to concatenate two string lists into one. To sort a string list, 使用 sort () 函数。

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"));
			

contains () function tells you whether the list contains a given string, while the indexOf () 函数返回 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", ...]
			

方法文档编制

QStringList.__init__ ( self )

Constructs an empty string list.

QStringList.__init__ ( self , QString  i )

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

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

另请参阅 append ().

QStringList.__init__ ( self , QStringList  l )

构造副本为 other string list.

This operation takes constant time 因为 QStringList is 隐式共享 , 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 .

另请参阅 operator= ().

QStringList.append ( self , QString  str )

QStringList.clear ( self )

bool QStringList.contains ( self , QString  str , Qt.CaseSensitivity   cs  = Qt.CaseSensitive)

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.

另请参阅 indexOf (), lastIndexOf (),和 QString.contains ().

int QStringList.count ( self , QString  str )

int QStringList.count ( self )

QStringList QStringList.filter ( self , QString  str , Qt.CaseSensitivity   cs  = Qt.CaseSensitive)

返回所有字符串的列表包含子字符串 str .

cs is Qt.CaseSensitive (默认), the string comparison is case sensitive; otherwise the comparison is case insensitive.

     QStringList list;
     list << "Bill Murray" << "John Doe" << "Bill Clinton";
     QStringList result;
     result = list.filter("Bill");
     // result: ["Bill Murray", "Bill Clinton"]
			

这相当于

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

另请参阅 contains ().

QStringList QStringList.filter ( self , QRegExp   rx )

这是重载函数。

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

QString QStringList.first ( self )

int QStringList.indexOf ( self , QString  value , int  from  = 0)

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.

另请参阅 lastIndexOf (), contains (),和 QRegExp.exactMatch ().

int QStringList.indexOf ( self , QRegExp   rx , int  from  = 0)

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.

另请参阅 lastIndexOf (), contains (),和 QList.indexOf ().

QStringList.insert ( self , int  i , QString  str )

bool QStringList.isEmpty ( self )

QString QStringList.join ( self , QString  sep )

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).

另请参阅 QString.split ().

QString QStringList.last ( self )

int QStringList.lastIndexOf ( self , QString  value , int  from  = -1)

Returns the index position of the last exact match of rx in the list, searching backward from index position from 。若 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.

另请参阅 indexOf (), contains (),和 QRegExp.exactMatch ().

int QStringList.lastIndexOf ( self , QRegExp   rx , int  from  = -1)

Returns the index position of the last occurrence of value in the list, searching backward from index position from 。若 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.

另请参阅 indexOf () and QList.lastIndexOf ().

QStringList QStringList.mid ( self , int  pos , int  length  = -1)

QStringList.move ( self , int  from , int  to )

QStringList.prepend ( self , QString  str )

int QStringList.removeAll ( self , QString  str )

QStringList.removeAt ( self , int  i )

int QStringList.removeDuplicates ( self )

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.

该函数在 Qt 4.5 引入。

QStringList.replace ( self , int  i , QString  str )

QStringList QStringList.replaceInStrings ( self , QString  before , 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 标志。

例如:

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

另请参阅 QString.replace ().

QStringList QStringList.replaceInStrings ( self , QRegExp   rx , QString  after )

这是重载函数。

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

例如:

     QStringList list;
     list << "alpha" << "beta" << "gamma" << "epsilon";
     list.replaceInStrings(QRegExp("^a"), "o");
     // 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), ...

例如:

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

QStringList.sort ( self )

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 使用 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.

另请参阅 qSort ().

QStringList.swap ( self , int  i , int  j )

QString QStringList.takeAt ( self , int  i )

QString QStringList.takeFirst ( self )

QString QStringList.takeLast ( self )

QStringList QStringList.__add__ ( self , QStringList  other )

int QStringList.__contains__ ( self , QString  str )

QStringList.__delitem__ ( self , int  i )

QStringList.__delitem__ ( self , slice  slice )

bool QStringList.__eq__ ( self , QStringList  other )

QString QStringList.__getitem__ ( self , int  i )

QStringList QStringList.__getitem__ ( self , slice  slice )

QStringList QStringList.__iadd__ ( self , QStringList  other )

QStringList QStringList.__iadd__ ( self , QString  value )

QStringList QStringList.__imul__ ( self , int  by )

QStringList.__len__ ( self )

QStringList QStringList.__lshift__ ( self , QString  str )

QStringList QStringList.__lshift__ ( self , QStringList  l )

QStringList QStringList.__mul__ ( self , int  by )

bool QStringList.__ne__ ( self , QStringList  other )

QStringList.__setitem__ ( self , int  i , QString  str )

QStringList.__setitem__ ( self , slice  slice , QStringList  list )