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 <xushitong@uniontech.com>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Shitong Xu 2024-12-23 10:54:21 +08:00
parent 1dcc5a6dd2
commit 34f1b6b6d1

View File

@ -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