[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).
This commit is contained in:
Yann Lanthony 2018-02-05 19:12:07 +01:00
parent 60f1f36ff8
commit 8823dba87f

View file

@ -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)