diff options
author | Samuel Gaist <samuel.gaist@idiap.ch> | 2018-11-19 21:37:37 +0100 |
---|---|---|
committer | Samuel Gaist <samuel.gaist@idiap.ch> | 2018-12-09 11:35:27 +0000 |
commit | b2297e595c43a5986a082c40443576436f8497d0 (patch) | |
tree | a5fa99f27bfdd674555e06293707e99e1da3497a | |
parent | fc116049649a6aef06f07dec3d89cc5d44c41723 (diff) | |
download | qtbase-b2297e595c43a5986a082c40443576436f8497d0.tar.gz |
QUrlQuery: Implement initializer list constructor
[ChangeLog][QtCore][QUrlQuery] QUrlQuery now provides an
initializer list constructor. It can be created using a list of
key/value pairs.
Fixes: QTBUG-68645
Change-Id: Ief5939aa477718f6dd3580f2c60f95ff3aa892ae
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/io/qurlquery.cpp | 8 | ||||
-rw-r--r-- | src/corelib/io/qurlquery.h | 9 | ||||
-rw-r--r-- | tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp | 8 |
3 files changed, 25 insertions, 0 deletions
diff --git a/src/corelib/io/qurlquery.cpp b/src/corelib/io/qurlquery.cpp index 97e7b8a4eb..10c3f836c8 100644 --- a/src/corelib/io/qurlquery.cpp +++ b/src/corelib/io/qurlquery.cpp @@ -145,6 +145,14 @@ QT_BEGIN_NAMESPACE \since 5.2 */ +/*! + \fn QUrlQuery(std::initializer_list<QPair<QString, QString>> list) + + \since 5.13 + + Constructs a QUrlQuery object from the \a list of key/value pair. +*/ + typedef QList<QPair<QString, QString> > Map; class QUrlQueryPrivate : public QSharedData diff --git a/src/corelib/io/qurlquery.h b/src/corelib/io/qurlquery.h index 092d002543..e3688aae2c 100644 --- a/src/corelib/io/qurlquery.h +++ b/src/corelib/io/qurlquery.h @@ -48,6 +48,8 @@ #include <QtCore/qstringlist.h> #endif +#include <initializer_list> + QT_BEGIN_NAMESPACE Q_CORE_EXPORT uint qHash(const QUrlQuery &key, uint seed = 0) Q_DECL_NOTHROW; @@ -59,6 +61,13 @@ public: QUrlQuery(); explicit QUrlQuery(const QUrl &url); explicit QUrlQuery(const QString &queryString); + QUrlQuery(std::initializer_list<QPair<QString, QString>> list) + : QUrlQuery() + { + for (const QPair<QString, QString> &item : list) + addQueryItem(item.first, item.second); + } + QUrlQuery(const QUrlQuery &other); QUrlQuery &operator=(const QUrlQuery &other); #ifdef Q_COMPILER_RVALUE_REFS diff --git a/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp b/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp index d839141091..25d392b37e 100644 --- a/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp +++ b/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp @@ -233,6 +233,14 @@ void tst_QUrlQuery::constructing() query += qMakePair(QString("prosent"), QString("%")); copy.setQueryItems(query); QVERIFY(!copy.isEmpty()); + + QUrlQuery fromList = { + {QString("type"), QString("login")}, + {QString("name"), QString::fromUtf8("åge nissemannsen")}, + {QString("ole&du"), QString::fromUtf8("anne+jørgen=sant")}, + {QString("prosent"), QString("%")} + }; + QCOMPARE(fromList, copy); } void tst_QUrlQuery::addRemove() |