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>
bb10
Samuel Gaist 2018-11-19 21:37:37 +01:00
parent fc11604964
commit b2297e595c
3 changed files with 25 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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()