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. stylesheet: The stylesheet to use.
update: Whether to update the stylesheet on config changes. update: Whether to update the stylesheet on config changes.
""" """
observer = StyleSheetObserver(obj, stylesheet=stylesheet) StyleSheetObserver(obj, stylesheet, update)
observer.register(update=update)
@functools.lru_cache() @functools.lru_cache()
@ -504,13 +503,19 @@ class StyleSheetObserver(QObject):
_stylesheet: The stylesheet template to use. _stylesheet: The stylesheet template to use.
""" """
def __init__(self, obj, stylesheet): def __init__(self, obj, stylesheet, update):
super().__init__(parent=obj) super().__init__()
self._obj = obj 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: if stylesheet is None:
self._stylesheet = obj.STYLESHEET self._stylesheet = obj.STYLESHEET
else: else:
self._stylesheet = stylesheet self._stylesheet = stylesheet
self._register()
def _get_stylesheet(self): def _get_stylesheet(self):
"""Format a stylesheet based on a template. """Format a stylesheet based on a template.
@ -525,7 +530,7 @@ class StyleSheetObserver(QObject):
"""Update the stylesheet for obj.""" """Update the stylesheet for obj."""
self._obj.setStyleSheet(self._get_stylesheet()) self._obj.setStyleSheet(self._get_stylesheet())
def register(self, update): def _register(self):
"""Do a first update and listen for more. """Do a first update and listen for more.
Args: Args:
@ -535,5 +540,5 @@ class StyleSheetObserver(QObject):
log.config.vdebug("stylesheet for {}: {}".format( log.config.vdebug("stylesheet for {}: {}".format(
self._obj.__class__.__name__, qss)) self._obj.__class__.__name__, qss))
self._obj.setStyleSheet(qss) self._obj.setStyleSheet(qss)
if update: if self.update:
instance.changed.connect(self._update_stylesheet) instance.changed.connect(self._update_stylesheet)

View File

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