QUrl: fromLocalFile(QString()) should lead to an empty URL.

This is much more useful than the URL "file:", it allows to use
"empty path" and "empty URL" for the same meaning (e.g. not set).

QFileDialog actually uses "file:" though, as the URL for the
"My Computer" item in the sidebar. This patch preserves that.

[ChangeLog][QtCore][QUrl] QUrl::fromLocalFile now returns an empty URL
if the input string is empty.

Change-Id: Ib5ce1a3cdf5f229368e5bcd83c62c1d1ac9f8a17
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
David Faure 2014-07-09 15:03:35 +02:00
parent 63efdf9851
commit fd331a5b3a
4 changed files with 13 additions and 3 deletions

View File

@ -3738,11 +3738,15 @@ bool QUrl::isDetached() const
"//servername/path/to/file.txt". Note that only certain platforms can
actually open this file using QFile::open().
An empty \a localFile leads to an empty URL (since Qt 5.4).
\sa toLocalFile(), isLocalFile(), QDir::toNativeSeparators()
*/
QUrl QUrl::fromLocalFile(const QString &localFile)
{
QUrl url;
if (localFile.isEmpty())
return url;
url.setScheme(fileScheme());
QString deslashified = QDir::fromNativeSeparators(localFile);

View File

@ -2736,7 +2736,7 @@ void QFileDialogPrivate::createWidgets()
qFileDialogUi->setupUi(q);
QList<QUrl> initialBookmarks;
initialBookmarks << QUrl::fromLocalFile(QLatin1String(""))
initialBookmarks << QUrl(QLatin1String("file:"))
<< QUrl::fromLocalFile(QDir::homePath());
qFileDialogUi->sidebar->setModelAndUrls(model, initialBookmarks);
QFileDialog::connect(qFileDialogUi->sidebar, SIGNAL(goToUrl(QUrl)),
@ -3713,7 +3713,7 @@ void QFileDialogComboBox::showPopup()
idx = idx.parent();
}
// add "my computer"
list.append(QUrl::fromLocalFile(QLatin1String("")));
list.append(QUrl(QLatin1String("file:")));
urlModel->addUrls(list, 0);
idx = model()->index(model()->rowCount() - 1, 0);

View File

@ -249,7 +249,8 @@ void QUrlModel::addUrls(const QList<QUrl> &list, int row, bool move)
continue;
//this makes sure the url is clean
const QString cleanUrl = QDir::cleanPath(url.toLocalFile());
url = QUrl::fromLocalFile(cleanUrl);
if (!cleanUrl.isEmpty())
url = QUrl::fromLocalFile(cleanUrl);
for (int j = 0; move && j < rowCount(); ++j) {
QString local = index(j, 0).data(UrlRole).toUrl().toLocalFile();

View File

@ -237,6 +237,11 @@ void tst_QUrl::constructing()
QVERIFY(url == url);
QVERIFY(!(url < url));
QUrl fromLocal = QUrl::fromLocalFile(QString());
QVERIFY(!fromLocal.isValid());
QVERIFY(fromLocal.isEmpty());
QCOMPARE(fromLocal.toString(), QString());
QUrl justHost("qt-project.org");
QVERIFY(!justHost.isEmpty());
QVERIFY(justHost.host().isEmpty());