diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp
index b974af80dc..98470829b0 100644
--- a/src/corelib/io/qfilesystemengine_unix.cpp
+++ b/src/corelib/io/qfilesystemengine_unix.cpp
@@ -1351,6 +1351,7 @@ bool QFileSystemEngine::renameFile(const QFileSystemEntry &source, const QFileSy
 //static
 bool QFileSystemEngine::renameOverwriteFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error)
 {
+    qDebug() << __FUNCTION__ << source.nativeFilePath() << target.nativeFilePath();
     if (Q_UNLIKELY(source.isEmpty() || target.isEmpty()))
         return emptyFileEntryWarning(), false;
     if (::rename(source.nativeFilePath().constData(), target.nativeFilePath().constData()) == 0)
diff --git a/src/corelib/io/qfilesystemengine_win.cpp b/src/corelib/io/qfilesystemengine_win.cpp
index fadc058110..302f77cd84 100644
--- a/src/corelib/io/qfilesystemengine_win.cpp
+++ b/src/corelib/io/qfilesystemengine_win.cpp
@@ -45,6 +45,7 @@
 #include "private/qfsfileengine_p.h"
 #include <private/qsystemlibrary_p.h>
 #include <qdebug.h>
+#include <qthread.h>
 
 #include "qfile.h"
 #include "qdir.h"
@@ -1379,11 +1380,19 @@ bool QFileSystemEngine::renameFile(const QFileSystemEntry &source, const QFileSy
 //static
 bool QFileSystemEngine::renameOverwriteFile(const QFileSystemEntry &source, const QFileSystemEntry &target, QSystemError &error)
 {
+
+    qDebug() << __FUNCTION__ << source.nativeFilePath() << target.nativeFilePath() << ">MoveFileEx"
+             << "msleep(20)";
+    QThread::msleep(10);
     bool ret = ::MoveFileEx(reinterpret_cast<const wchar_t *>(source.nativeFilePath().utf16()),
                             reinterpret_cast<const wchar_t *>(target.nativeFilePath().utf16()),
                             MOVEFILE_REPLACE_EXISTING) != 0;
-    if (!ret)
-        error = QSystemError(::GetLastError(), QSystemError::NativeError);
+
+    if (!ret) {
+        DWORD er =::GetLastError();
+        qDebug() << __FUNCTION__ << "<MoveFileEx fails" << er;
+        error = QSystemError(er, QSystemError::NativeError);
+    }
     return ret;
 }
 
diff --git a/src/corelib/io/qfsfileengine_win.cpp b/src/corelib/io/qfsfileengine_win.cpp
index 759effe632..7ae54953f0 100644
--- a/src/corelib/io/qfsfileengine_win.cpp
+++ b/src/corelib/io/qfsfileengine_win.cpp
@@ -110,6 +110,8 @@ bool QFSFileEnginePrivate::nativeOpen(QIODevice::OpenMode openMode)
 
     // All files are opened in share mode (both read and write).
     DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
+    fprintf(stderr, "%s %s shareMode=0x%x\n", __FUNCTION__, fileEntry.nativeFilePath().toLocal8Bit().constData(),
+            int(shareMode));
 
     int accessRights = 0;
     if (openMode & QIODevice::ReadOnly)
@@ -474,6 +476,7 @@ bool QFSFileEngine::rename(const QString &newName)
 bool QFSFileEngine::renameOverwrite(const QString &newName)
 {
     Q_D(QFSFileEngine);
+    qDebug() << __FUNCTION__ << newName;
     QSystemError error;
     bool ret = QFileSystemEngine::renameOverwriteFile(d->fileEntry, QFileSystemEntry(newName), error);
     if (!ret)
diff --git a/src/corelib/io/qsavefile.cpp b/src/corelib/io/qsavefile.cpp
index 63f2284ef5..8467bad3ac 100644
--- a/src/corelib/io/qsavefile.cpp
+++ b/src/corelib/io/qsavefile.cpp
@@ -262,6 +262,7 @@ bool QSaveFile::open(OpenMode mode)
     }
 #endif
 
+    qDebug() << __FUNCTION__ << "QTemporaryFileEngine" << d->finalFileName;
     d->fileEngine = new QTemporaryFileEngine(&d->finalFileName);
     // if the target file exists, we'll copy its permissions below,
     // but until then, let's ensure the temporary file is not accessible
@@ -327,14 +328,20 @@ bool QSaveFile::commit()
         qWarning("QSaveFile::commit: File (%s) is not open", qPrintable(fileName()));
         return false;
     }
+    qDebug(">close");
     QFileDevice::close(); // calls flush()
+    qDebug(">close");
 
     // Sync to disk if possible. Ignore errors (e.g. not supported).
+    qDebug(">syncToDisk");
     d->fileEngine->syncToDisk();
+    qDebug("<syncToDisk");
 
     if (d->useTemporaryFile) {
         if (d->writeError != QFileDevice::NoError) {
+            qDebug(">remove");
             d->fileEngine->remove();
+            qDebug("<remove");
             d->writeError = QFileDevice::NoError;
             delete d->fileEngine;
             d->fileEngine = 0;
@@ -343,9 +350,13 @@ bool QSaveFile::commit()
         // atomically replace old file with new file
         // Can't use QFile::rename for that, must use the file engine directly
         Q_ASSERT(d->fileEngine);
+        qDebug(">renameOverwrite");
         if (!d->fileEngine->renameOverwrite(d->finalFileName)) {
+            qDebug("<renameOverwrite");
             d->setError(d->fileEngine->error(), d->fileEngine->errorString());
+            qDebug(">remove");
             d->fileEngine->remove();
+            qDebug("<remove");
             delete d->fileEngine;
             d->fileEngine = 0;
             return false;
diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
index 35699d52df..776bde352f 100644
--- a/src/corelib/io/qtemporaryfile.cpp
+++ b/src/corelib/io/qtemporaryfile.cpp
@@ -42,6 +42,7 @@
 
 #include "qplatformdefs.h"
 #include "qrandom.h"
+#include "qdebug.h"
 #include "private/qtemporaryfile_p.h"
 #include "private/qfile_p.h"
 #include "private/qsystemerror_p.h"
@@ -217,11 +218,22 @@ static bool createFileFromTemplate(NativeFileHandle &file, QTemporaryFileName &t
 #if defined(Q_OS_WIN)
         Q_UNUSED(mode);
 
+        DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
+        DWORD flagsAndAttributes =  FILE_ATTRIBUTE_NORMAL;
+        if (qEnvironmentVariableIsSet("HIDDEN"))
+            flagsAndAttributes |= FILE_ATTRIBUTE_HIDDEN;
+        if (qEnvironmentVariableIsSet("NONSHARE"))
+            shareMode=0;
+
+        qDebug().noquote() << __FUNCTION__ << path << "(mode=0x" << hex << mode
+                 << "),\n   shareMode=0x" <<  shareMode
+                 << "FlagsAndAttributes=0x" << flagsAndAttributes;
+
 #  ifndef Q_OS_WINRT
         file = CreateFile((const wchar_t *)path.constData(),
                 GENERIC_READ | GENERIC_WRITE,
-                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_NEW,
-                FILE_ATTRIBUTE_NORMAL, NULL);
+                shareMode, NULL, CREATE_NEW,
+                flagsAndAttributes, NULL);
 #  else // !Q_OS_WINRT
         file = CreateFile2((const wchar_t *)path.constData(),
                 GENERIC_READ | GENERIC_WRITE,
