diff --git a/autotests/kfiledialog_unittest.cpp b/autotests/kfiledialog_unittest.cpp --- a/autotests/kfiledialog_unittest.cpp +++ b/autotests/kfiledialog_unittest.cpp @@ -77,6 +77,24 @@ QCOMPARE(dialog.directory().absolutePath(), QDir::rootPath()); } + void testSelectUrl() + { + QTemporaryFile tempFile(QDir::tempPath()+"/kfiledialogtest_XXXXXX"); + tempFile.setAutoRemove(true); + tempFile.open(); + QString tempName = tempFile.fileName(); + QUrl url = QUrl::fromLocalFile(tempName); + int idx = tempName.lastIndexOf('/'); + QUrl directoryUrl = QUrl::fromLocalFile(tempName.left(idx+1)); + + QFileDialog dialog; + dialog.selectUrl(url); + dialog.show(); + + // check if dialog was set to base directory url of the passed file url + QCOMPARE(dialog.directoryUrl(), directoryUrl); + } + void testViewMode() { // Open a file dialog, and change view mode to tree diff --git a/src/platformtheme/kdeplatformfiledialoghelper.cpp b/src/platformtheme/kdeplatformfiledialoghelper.cpp --- a/src/platformtheme/kdeplatformfiledialoghelper.cpp +++ b/src/platformtheme/kdeplatformfiledialoghelper.cpp @@ -361,6 +361,13 @@ void KDEPlatformFileDialogHelper::selectFile(const QUrl &filename) { m_dialog->selectFile(filename); + + // Qt 5 at least until 5.7.0 does not derive the directory from the passed url + // and set the initialDirectory option accordingly, also not for known schemes + // like file://, so we have to do it ourselves + QSharedPointer opt(new QFileDialogOptions(*options())); + opt->setInitialDirectory(m_dialog->directory()); + setOptions(opt); } void KDEPlatformFileDialogHelper::setDirectory(const QUrl &directory)