Skip to content

Commit

Permalink
fix(qml): Fix QML auto-reloading even if path removal fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Feb 18, 2024
1 parent 5a792d8 commit 83204ce
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/qml/qmlautoreload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,33 @@ QmlAutoReload::QmlAutoReload() {
}

QUrl QmlAutoReload::intercept(const QUrl& url, QQmlAbstractUrlInterceptor::DataType type) {
if (!url.isLocalFile() || !QFileInfo(url.toLocalFile()).isFile()) {
const auto path = url.toLocalFile();

if (!url.isLocalFile() || !QFileInfo(path).isFile()) {
return url;
}

// Do not add path if it's already watched. See note in the
// `QFileSystemWatcher::fileChanged()` documentation.
if (m_fileWatcher.files().contains(path)) {
return url;
}
m_fileWatcher.addPath(url.toLocalFile());

m_fileWatcher.addPath(path);
return url;
}

void QmlAutoReload::slotFileChanged(const QString& changedFile) {
qDebug() << "File" << changedFile << "used in QML interface has been changed.";

// This is to prevent double-reload when a file is updated twice
// in a row as part of the normal saving process. See note in
// QFileSystemWatcher::fileChanged documentation.
if (m_fileWatcher.removePath(changedFile)) {
emit triggered();
}
// in a row as part of the normal saving process. See note in the
// `QFileSystemWatcher::fileChanged()` documentation.
//
// Unfortunately we cannot be sure that `QFileSystemWatcher::removePath()`
// actually returns true, so we need to emit the signal anyway.
m_fileWatcher.removePath(changedFile);
emit triggered();
}

void QmlAutoReload::clear() {
Expand Down

0 comments on commit 83204ce

Please sign in to comment.