From 8823dba87fab67e1dedebdc1e61cea30837ee3ad Mon Sep 17 00:00:00 2001 From: Yann Lanthony Date: Mon, 5 Feb 2018 19:12:07 +0100 Subject: [PATCH] [utils] QmlInstantEngine: fix spurious consecutive reloads issue Fix the problem with the application taking up all the CPU after a few reloads while using a Qt3D Scene. When a file changes, wait for a few milliseconds before adding it back to the watch list. This prevents the system to reload the engine twice for a same fileChanged event (happens with some text editors). --- meshroom/ui/utils.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/meshroom/ui/utils.py b/meshroom/ui/utils.py index 55762719..7f13c3fb 100755 --- a/meshroom/ui/utils.py +++ b/meshroom/ui/utils.py @@ -1,7 +1,7 @@ import os import time -from PySide2.QtCore import QFileSystemWatcher, QUrl, Slot +from PySide2.QtCore import QFileSystemWatcher, QUrl, Slot, QTimer from PySide2.QtQml import QQmlApplicationEngine @@ -29,6 +29,8 @@ class QmlInstantEngine(QQmlApplicationEngine): self._rootItem = None def onObjectCreated(root, url): + if not root: + return # Restore root item geometry if self._rootItem: root.setGeometry(self._rootItem.geometry()) @@ -160,6 +162,11 @@ class QmlInstantEngine(QQmlApplicationEngine): @Slot(str) def onFileChanged(self, filepath): """ Handle changes in a watched file. """ + if filepath not in self._watchedFiles: + # could happen if a file has just been reloaded + # and has not been re-added yet to the watched files + return + if self._verbose: print("Source file changed : ", filepath) # Clear the QQuickEngine cache @@ -177,9 +184,10 @@ class QmlInstantEngine(QQmlApplicationEngine): self.reload() - # Finally, read the modified file to the watch system - self.addFile(filepath) + # Finally, re-add the modified file to the watch system + # after a short cooldown to avoid multiple consecutive reloads + QTimer.singleShot(200, lambda: self.addFile(filepath)) def reload(self): - print("Reloading ", self._sourceFile) + print("Reloading {}".format(self._sourceFile)) self.load(self._sourceFile)