diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2019-09-05 09:52:05 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2019-09-10 12:47:49 +0200 |
commit | c0b3c06a7d4f6a2f90742e8dc81c898082a70416 (patch) | |
tree | 42163719cedc3e79836acf2b58043ad9796102fd /src/widgets/dialogs/qfilesystemmodel.cpp | |
parent | 648dac1ceb40a346adf2081997f5c6166c3ad503 (diff) |
QFileDialog: Fix volume name display on Windows
The volume names displayed did not match those of Windows
Explorer; for example "New Volume (X:)" would be displayed for
mapped network drives.
Replace GetVolumeInformation() and manual formatting by the
normal display name of IShellItem.
Fixes: QTBUG-78043
Change-Id: Ia742b7733e8ddc31e9506f15d90d065b985a111d
Reviewed-by: André de la Rocha <andre.rocha@qt.io>
Diffstat (limited to 'src/widgets/dialogs/qfilesystemmodel.cpp')
-rw-r--r-- | src/widgets/dialogs/qfilesystemmodel.cpp | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index e9e49e0c33..a04189513a 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -57,6 +57,9 @@ #ifdef Q_OS_WIN # include <QtCore/QVarLengthArray> # include <qt_windows.h> +# ifndef Q_OS_WINRT +# include <shlobj.h> +# endif #endif QT_BEGIN_NAMESPACE @@ -837,8 +840,8 @@ QString QFileSystemModelPrivate::displayName(const QModelIndex &index) const { #if defined(Q_OS_WIN) QFileSystemNode *dirNode = node(index); - if (!dirNode->volumeName.isNull()) - return dirNode->volumeName + QLatin1String(" (") + name(index) + QLatin1Char(')'); + if (!dirNode->volumeName.isEmpty()) + return dirNode->volumeName; #endif return name(index); } @@ -1773,6 +1776,27 @@ void QFileSystemModelPrivate::_q_directoryChanged(const QString &directory, cons removeNode(parentNode, toRemove[i]); } +#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +static QString volumeName(const QString &path) +{ + IShellItem *item = nullptr; + const QString native = QDir::toNativeSeparators(path); + HRESULT hr = SHCreateItemFromParsingName(reinterpret_cast<const wchar_t *>(native.utf16()), + nullptr, IID_IShellItem, + reinterpret_cast<void **>(&item)); + if (FAILED(hr)) + return QString(); + LPWSTR name = nullptr; + hr = item->GetDisplayName(SIGDN_NORMALDISPLAY, &name); + if (FAILED(hr)) + return QString(); + QString result = QString::fromWCharArray(name); + CoTaskMemFree(name); + item->Release(); + return result; +} +#endif // Q_OS_WIN && !Q_OS_WINRT + /*! \internal @@ -1791,15 +1815,8 @@ QFileSystemModelPrivate::QFileSystemNode* QFileSystemModelPrivate::addNode(QFile #endif #if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) //The parentNode is "" so we are listing the drives - if (parentNode->fileName.isEmpty()) { - wchar_t name[MAX_PATH + 1]; - //GetVolumeInformation requires to add trailing backslash - const QString nodeName = fileName + QLatin1String("\\"); - BOOL success = ::GetVolumeInformation((wchar_t *)(nodeName.utf16()), - name, MAX_PATH + 1, NULL, 0, NULL, NULL, 0); - if (success && name[0]) - node->volumeName = QString::fromWCharArray(name); - } + if (parentNode->fileName.isEmpty()) + node->volumeName = volumeName(fileName); #endif Q_ASSERT(!parentNode->children.contains(fileName)); parentNode->children.insert(fileName, node); |