From 34f1b6b6d1dd7c37f2d71c0032ddc0c83360c11c Mon Sep 17 00:00:00 2001 From: Shitong Xu Date: Mon, 23 Dec 2024 10:54:21 +0800 Subject: [PATCH] QFileDialog: the lastVisited not loaded correctly Url passed from the QFileDialogArgs will never be invalid, it will always be the value that user passed into or the lastVisited or the working directory. And because of this, the fixes code of QTBUG-70798 will never be invoked, and the `lastVisited` will never be initialize if nativedialog is in use. I wrote an init function to initialize the `lastVisited` global variable, to make sure the value will be loaded correctly Fixes: QTBUG-70798 Pick-to: 6.8 6.9 Change-Id: Id05f63db7b4738e6317721741111882a565437e3 Reviewed-by: Xu Shitong Reviewed-by: Richard Moe Gustavsen --- src/widgets/dialogs/qfiledialog.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index d52d678243e..b3a97d07351 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -2612,6 +2612,16 @@ inline static QUrl _qt_get_directory(const QUrl &url, const QFileInfo &local) } } +inline static void _qt_init_lastVisited() { +#if QT_CONFIG(settings) + if (lastVisitedDir()->isEmpty()) { + QSettings settings(QSettings::UserScope, u"QtProject"_s); + const QString &lastVisisted = settings.value("FileDialog/lastVisited", QString()).toString(); + *lastVisitedDir() = QUrl::fromLocalFile(lastVisisted); + } +#endif +} + /* Initialize working directory and selection from \a url. */ @@ -2623,6 +2633,7 @@ QFileDialogArgs::QFileDialogArgs(const QUrl &url) if (!url.isEmpty()) directory = _qt_get_directory(url, local); if (directory.isEmpty()) { + _qt_init_lastVisited(); const QUrl lastVisited = *lastVisitedDir(); if (lastVisited != url) directory = _qt_get_directory(lastVisited, QFileInfo()); @@ -2910,11 +2921,7 @@ void QFileDialogPrivate::init(const QFileDialogArgs &args) q->setFileMode(QFileDialog::AnyFile); if (!args.filter.isEmpty()) q->setNameFilter(args.filter); - // QTBUG-70798, prevent the default blocking the restore logic. - const bool dontStoreDir = !args.directory.isValid() && !lastVisitedDir()->isValid(); q->setDirectoryUrl(args.directory); - if (dontStoreDir) - lastVisitedDir()->clear(); if (args.directory.isLocalFile()) q->selectFile(args.selection); else