+ QString const qfilename = toqstr(filename_);
+ if (!qwatcher_->files().contains(qfilename)) {
+ bool const existed = exists_;
+ exists_ = QFile(qfilename).exists();
+#if (QT_VERSION >= 0x050000)
+ if (exists_ && !qwatcher_->addPath(qfilename))
+#else
+ auto add_path = [&]() {
+ qwatcher_->addPath(qfilename);
+ return qwatcher_->files().contains(qfilename);
+ };
+ if (exists_ && !add_path())
+#endif
+ {
+ LYXERR(Debug::FILES,
+ "Could not add path to QFileSystemWatcher: " << filename_);
+ QTimer::singleShot(5000, this, SLOT(refresh()));
+ } else {
+ if (!exists_)
+ // The standard way to overwrite a file is to delete it and
+ // create a new file with the same name. Therefore if the file
+ // has just been deleted, it is smart to check not too long
+ // after whether it has been recreated.
+ QTimer::singleShot(existed ? 100 : 2000, this, SLOT(refresh()));
+ if (existed != exists_ && emit)
+ Q_EMIT fileChanged(exists_);
+ }
+ }
+}