diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp index ac17de1bee..27565a7878 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp @@ -54,11 +54,14 @@ QList dateList; //! [0] -//! [1] -QList list; -list << "one" << "two" << "three"; -// list: ["one", "two", "three"] -//! [1] +//! [1a] +QList list = { "one", "two", "three" }; +//! [1a] + + +//! [1b] +list << "four" << "five"; +//! [1b] //! [2] diff --git a/src/corelib/doc/snippets/qstringlist/main.cpp b/src/corelib/doc/snippets/qstringlist/main.cpp index 4d9c015747..55c60650fe 100644 --- a/src/corelib/doc/snippets/qstringlist/main.cpp +++ b/src/corelib/doc/snippets/qstringlist/main.cpp @@ -61,10 +61,13 @@ public: Widget::Widget(QWidget *parent) : QWidget(parent) { -//! [0] - QStringList fonts; - fonts << "Arial" << "Helvetica" << "Times" << "Courier"; -//! [0] +//! [0a] + QStringList fonts = { "Arial", "Helvetica", "Times" }; +//! [0a] + +//! [0b] + fonts << "Courier" << "Verdana"; +//! [0b] //! [1] for (int i = 0; i < fonts.size(); ++i) diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index 33835e3d28..17aba8035b 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -408,15 +408,20 @@ void **QListData::erase(void **xi) from strings. QList stores a list of items. The default constructor creates an - empty list. To insert items into the list, you can use - operator<<(): + empty list. You can use the initializer-list constructor to create + a list with elements: - \snippet code/src_corelib_tools_qlistdata.cpp 1 + \snippet code/src_corelib_tools_qlistdata.cpp 1a QList provides these basic functions to add, move, and remove items: insert(), replace(), removeAt(), move(), and swap(). In addition, it provides the following convenience functions: - append(), prepend(), removeFirst(), and removeLast(). + append(), \l{operator<<()}, \l{operator+=()}, prepend(), removeFirst(), + and removeLast(). + + \l{operator<<()} allows to conveniently add multiple elements to a list: + + \snippet code/src_corelib_tools_qlistdata.cpp 1b QList uses 0-based indexes, just like C++ arrays. To access the item at a particular index position, you can use operator[](). On diff --git a/src/corelib/tools/qstringlist.cpp b/src/corelib/tools/qstringlist.cpp index d10d9ad9d0..c9db39a29f 100644 --- a/src/corelib/tools/qstringlist.cpp +++ b/src/corelib/tools/qstringlist.cpp @@ -98,14 +98,25 @@ QT_BEGIN_NAMESPACE \tableofcontents + \section1 Initializing + + The default constructor creates an empty list. You can use the + initializer-list constructor to create a list with elements: + + \snippet qstringlist/main.cpp 0a + \section1 Adding Strings Strings can be added to a list using the \l + {QList::insert()}{insert()} \l {QList::append()}{append()}, \l {QList::operator+=()}{operator+=()} and \l - {QStringList::operator<<()}{operator<<()} functions. For example: + {operator<<()} functions. - \snippet qstringlist/main.cpp 0 + \l{operator<<()} can be used to + conveniently add multiple elements to a list: + + \snippet qstringlist/main.cpp 0b \section1 Iterating Over the Strings