Skip to content

Commit

Permalink
QFileSystemModel fails to locate a host from root's visible children
Browse files Browse the repository at this point in the history
In QFileSystemModel, in some cases the hostname in a UNC path is
converted to lower case and stored in the root node's visibleChildren.
When QFileSystemModel sets the UNC path as the root path, it tries to
get the row number for the host, but it didn't convert the hostname to
lower case before getting the row number, which resulted in the host
not found in the root node's visible children. As a result, it returns
-1, an invalid row number. Change the behavior to find the node for the
host using the host name case-insensitive and then get the row number.

Fixes: QTBUG-71701
Pick-to: 5.15 6.0 6.1
Change-Id: Ib95c7b6d2bc22fd82f2789b7004b6fc82dfcb13b
Reviewed-by: Edward Welbourne <[email protected]>
Reviewed-by: Qt CI Bot <[email protected]>
Reviewed-by: Alex Blasche <[email protected]>
  • Loading branch information
dongmeiwang authored and kaheimri committed May 20, 2021
1 parent c2258e8 commit e253a30
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/gui/itemmodels/qfilesystemmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,11 @@ QFileSystemModelPrivate::QFileSystemNode *QFileSystemModelPrivate::node(const QS
if (absolutePath.endsWith(QLatin1Char('/')))
trailingSeparator = QLatin1String("\\");
int r = 0;
QFileSystemModelPrivate::QFileSystemNode *rootNode = const_cast<QFileSystemModelPrivate::QFileSystemNode*>(&root);
if (!root.children.contains(host.toLower())) {
auto rootNode = const_cast<QFileSystemModelPrivate::QFileSystemNode*>(&root);
auto it = root.children.constFind(host);
if (it != root.children.cend()) {
host = it.key(); // Normalize case for lookup in visibleLocation()
} else {
if (pathElements.count() == 1 && !absolutePath.endsWith(QLatin1Char('/')))
return rootNode;
QFileInfo info(host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ void tst_QFileSystemModel::rootPath()
QCOMPARE(rootChanged.count(), oldCount + 1);
QCOMPARE(model->rootDirectory().absolutePath(), newdir.path());
}

#ifdef Q_OS_WIN
// check case insensitive root node on windows, tests QTBUG-71701
QModelIndex index = model->setRootPath(uR"(\\localhost\c$)"_qs);
QVERIFY(index.isValid());
QCOMPARE(model->rootPath(), u"//localhost/c$"_qs);

index = model->setRootPath(uR"(\\localhost\C$)"_qs);
QVERIFY(index.isValid());
QCOMPARE(model->rootPath(), u"//localhost/C$"_qs);

index = model->setRootPath(uR"(\\LOCALHOST\C$)"_qs);
QVERIFY(index.isValid());
QCOMPARE(model->rootPath(), u"//LOCALHOST/C$"_qs);
#endif
}

void tst_QFileSystemModel::readOnly()
Expand Down

0 comments on commit e253a30

Please sign in to comment.