QUrl: methods for converting QStringList <-> QList<QUrl>
This is a very common thing to do, e.g. in order to send urls via DBus. Change-Id: I277902460ee1ad6780446e862e86b3c2eb8c5315 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
327b2ba3b7
commit
6b9545a980
@ -3764,6 +3764,37 @@ QString QUrl::errorString() const
|
||||
return msg;
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.1
|
||||
|
||||
Converts a list of \a urls into a list of QStrings, using toString(\a options).
|
||||
*/
|
||||
QStringList QUrl::toStringList(const QList<QUrl> &urls, FormattingOptions options)
|
||||
{
|
||||
QStringList lst;
|
||||
lst.reserve(urls.size());
|
||||
foreach (const QUrl &url, urls)
|
||||
lst.append(url.toString(options));
|
||||
return lst;
|
||||
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.1
|
||||
|
||||
Converts a list of strings representing \a urls into a list of urls, using QUrl(str, \a mode).
|
||||
Note that this means all strings must be urls, not for instance local paths.
|
||||
*/
|
||||
QList<QUrl> QUrl::fromStringList(const QStringList &urls, ParsingMode mode)
|
||||
{
|
||||
QList<QUrl> lst;
|
||||
lst.reserve(urls.size());
|
||||
foreach (const QString &str, urls) {
|
||||
lst.append(QUrl(str, mode));
|
||||
}
|
||||
return lst;
|
||||
}
|
||||
|
||||
/*!
|
||||
\typedef QUrl::DataPtr
|
||||
\internal
|
||||
|
@ -322,6 +322,9 @@ public:
|
||||
static QString fromAce(const QByteArray &);
|
||||
static QByteArray toAce(const QString &);
|
||||
static QStringList idnWhitelist();
|
||||
static QStringList toStringList(const QList<QUrl> &uris, FormattingOptions options = FormattingOptions(PrettyDecoded));
|
||||
static QList<QUrl> fromStringList(const QStringList &uris, ParsingMode mode = TolerantMode);
|
||||
|
||||
static void setIdnWhitelist(const QStringList &);
|
||||
friend Q_CORE_EXPORT uint qHash(const QUrl &url, uint seed = 0) Q_DECL_NOTHROW;
|
||||
|
||||
|
@ -79,6 +79,8 @@ private slots:
|
||||
void toString();
|
||||
void toString_constructed_data();
|
||||
void toString_constructed();
|
||||
void toAndFromStringList_data();
|
||||
void toAndFromStringList();
|
||||
void isParentOf_data();
|
||||
void isParentOf();
|
||||
void toLocalFile_data();
|
||||
@ -941,6 +943,25 @@ void tst_QUrl::toString()
|
||||
QCOMPARE(url.toString(QUrl::FormattingOptions(options)), string);
|
||||
}
|
||||
|
||||
void tst_QUrl::toAndFromStringList_data()
|
||||
{
|
||||
QTest::addColumn<QStringList>("strings");
|
||||
|
||||
QTest::newRow("empty") << QStringList();
|
||||
QTest::newRow("local") << (QStringList() << "file:///tmp" << "file:///");
|
||||
QTest::newRow("remote") << (QStringList() << "http://qt-project.org");
|
||||
}
|
||||
|
||||
void tst_QUrl::toAndFromStringList()
|
||||
{
|
||||
QFETCH(QStringList, strings);
|
||||
|
||||
const QList<QUrl> urls = QUrl::fromStringList(strings);
|
||||
QCOMPARE(urls.count(), strings.count());
|
||||
const QStringList converted = QUrl::toStringList(urls);
|
||||
QCOMPARE(converted, strings);
|
||||
}
|
||||
|
||||
//### more tests ... what do we expect ...
|
||||
void tst_QUrl::isParentOf_data()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user