Fix garbage collection of StyleSheetObserver objects

This commit is contained in:
Jay Kamat 2017-11-10 22:39:10 -05:00
parent c47f0402ab
commit e00a072d15
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
2 changed files with 12 additions and 7 deletions

View File

@ -483,8 +483,7 @@ def set_register_stylesheet(obj, *, stylesheet=None, update=True):
stylesheet: The stylesheet to use.
update: Whether to update the stylesheet on config changes.
"""
observer = StyleSheetObserver(obj, stylesheet=stylesheet)
observer.register(update=update)
StyleSheetObserver(obj, stylesheet, update)
@functools.lru_cache()
@ -504,13 +503,19 @@ class StyleSheetObserver(QObject):
_stylesheet: The stylesheet template to use.
"""
def __init__(self, obj, stylesheet):
super().__init__(parent=obj)
def __init__(self, obj, stylesheet, update):
super().__init__()
self._obj = obj
self.update = update
# We only need to hang around if we are asked to update.
if self.update:
self.setParent(self._obj)
if stylesheet is None:
self._stylesheet = obj.STYLESHEET
else:
self._stylesheet = stylesheet
self._register()
def _get_stylesheet(self):
"""Format a stylesheet based on a template.
@ -525,7 +530,7 @@ class StyleSheetObserver(QObject):
"""Update the stylesheet for obj."""
self._obj.setStyleSheet(self._get_stylesheet())
def register(self, update):
def _register(self):
"""Do a first update and listen for more.
Args:
@ -535,5 +540,5 @@ class StyleSheetObserver(QObject):
log.config.vdebug("stylesheet for {}: {}".format(
self._obj.__class__.__name__, qss))
self._obj.setStyleSheet(qss)
if update:
if self.update:
instance.changed.connect(self._update_stylesheet)

View File

@ -601,7 +601,7 @@ class StyleObj(QObject):
def test_get_stylesheet(config_stub):
config_stub.val.colors.hints.fg = 'magenta'
observer = config.StyleSheetObserver(
StyleObj(), stylesheet="{{ conf.colors.hints.fg }}")
StyleObj(), stylesheet="{{ conf.colors.hints.fg }}", update=False)
assert observer._get_stylesheet() == 'magenta'