diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp
index 69496dbd29..612a22ee43 100644
--- a/src/widgets/dialogs/qfiledialog.cpp
+++ b/src/widgets/dialogs/qfiledialog.cpp
@@ -349,6 +349,7 @@ QT_END_INCLUDE_NAMESPACE
 QFileDialog::QFileDialog(QWidget *parent, Qt::WindowFlags f)
     : QDialog(*new QFileDialogPrivate, parent, f)
 {
+    qDebug() << __FUNCTION__  << '1';
     Q_D(QFileDialog);
     d->init();
 }
@@ -366,8 +367,9 @@ QFileDialog::QFileDialog(QWidget *parent,
                      const QString &filter)
     : QDialog(*new QFileDialogPrivate, parent, 0)
 {
+    qDebug() << __FUNCTION__ << '2' << directory;
     Q_D(QFileDialog);
-    d->init(QUrl::fromLocalFile(directory), filter, caption);
+    d->init(directory.isEmpty() ? QUrl() : QUrl::fromLocalFile(directory), filter, caption);
 }
 
 /*!
@@ -377,6 +379,7 @@ QFileDialog::QFileDialog(const QFileDialogArgs &args)
     : QDialog(*new QFileDialogPrivate, args.parent, 0)
 {
     Q_D(QFileDialog);
+    qDebug() << __FUNCTION__ << '3' << args.directory;
     d->init(args.directory, args.filter, args.caption);
     setFileMode(args.mode);
     setOptions(args.options);
@@ -471,6 +474,7 @@ QByteArray QFileDialog::saveState() const
 */
 bool QFileDialog::restoreState(const QByteArray &state)
 {
+    qDebug() << __FUNCTION__ << state.size();
     Q_D(QFileDialog);
     QByteArray sd = state;
     QDataStream stream(&sd, QIODevice::ReadOnly);
@@ -500,7 +504,10 @@ bool QFileDialog::restoreState(const QByteArray &state)
     stream >> d->headerData
            >> viewMode;
 
+    qDebug() << __FUNCTION__ << "currentDirectory=" << currentDirectory <<
+                "lastVisitedDir" <<  *lastVisitedDir();
     setDirectoryUrl(lastVisitedDir()->isEmpty() ? currentDirectory : *lastVisitedDir());
+
     setViewMode(static_cast<QFileDialog::ViewMode>(viewMode));
 
     if (!d->usingWidgets())
@@ -989,6 +996,7 @@ QDir QFileDialog::directory() const
 */
 void QFileDialog::setDirectoryUrl(const QUrl &directory)
 {
+    qDebug() << __FUNCTION__ << directory << "empty=" << directory.isEmpty() << "valid=" << directory.isValid();
     Q_D(QFileDialog);
     if (!directory.isValid())
         return;
@@ -2579,11 +2587,13 @@ inline static QUrl _qt_get_directory(const QUrl &url)
  */
 QUrl QFileDialogPrivate::workingDirectory(const QUrl &url)
 {
+    qDebug() << __FUNCTION__ << '>' << url << "empty=" << url.isEmpty() << "valid=" << url.isValid();
     if (!url.isEmpty()) {
         QUrl directory = _qt_get_directory(url);
         if (!directory.isEmpty())
             return directory;
     }
+    qDebug() << __FUNCTION__ << "def";
     QUrl directory = _qt_get_directory(*lastVisitedDir());
     if (!directory.isEmpty())
         return directory;
@@ -2744,6 +2754,7 @@ void QFileDialog::accept()
 #if QT_CONFIG(settings)
 void QFileDialogPrivate::saveSettings()
 {
+    qDebug() << __FUNCTION__;
     Q_Q(QFileDialog);
     QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
     settings.beginGroup(QLatin1String("FileDialog"));
@@ -2759,6 +2770,7 @@ void QFileDialogPrivate::saveSettings()
     for (const QString &path : history)
         historyUrls << QUrl::fromLocalFile(path).toString();
     settings.setValue(QLatin1String("history"), historyUrls);
+    qDebug() << __FUNCTION__ << "lastVisitedDir=" << lastVisitedDir()->toString();
     settings.setValue(QLatin1String("lastVisited"), lastVisitedDir()->toString());
     const QMetaEnum &viewModeMeta = q->metaObject()->enumerator(q->metaObject()->indexOfEnumerator("ViewMode"));
     settings.setValue(QLatin1String("viewMode"), QLatin1String(viewModeMeta.key(q->viewMode())));
@@ -2773,6 +2785,8 @@ bool QFileDialogPrivate::restoreFromSettings()
         return false;
     settings.beginGroup(QLatin1String("FileDialog"));
 
+    qDebug() << __FUNCTION__ <<  "lastVisitedDir=" << *lastVisitedDir() << "settings: "
+             << settings.value(QLatin1String("lastVisited"));
     q->setDirectoryUrl(lastVisitedDir()->isEmpty() ? settings.value(QLatin1String("lastVisited")).toUrl() : *lastVisitedDir());
 
     QByteArray viewModeStr = settings.value(QLatin1String("viewMode")).toString().toLatin1();
@@ -2852,6 +2866,7 @@ bool QFileDialogPrivate::restoreWidgetState(QStringList &history, int splitterPo
 void QFileDialogPrivate::init(const QUrl &directory, const QString &nameFilter,
                               const QString &caption)
 {
+    qDebug() << __FUNCTION__ << directory;
     Q_Q(QFileDialog);
     if (!caption.isEmpty()) {
         useDefaultCaption = false;
@@ -2867,6 +2882,8 @@ void QFileDialogPrivate::init(const QUrl &directory, const QString &nameFilter,
     if (!nameFilter.isEmpty())
         q->setNameFilter(nameFilter);
     q->setDirectoryUrl(workingDirectory(directory));
+//    if (!directory.isValid())
+//        lastVisitedDir()->clear();
     if (directory.isLocalFile())
         q->selectFile(initialSelection(directory));
     else
