Make it possible to update an object in the object registry.
This commit is contained in:
parent
908a69af18
commit
a2d3ca6565
@ -395,12 +395,8 @@ class ObjectRegistry(collections.UserDict):
|
||||
def __setitem__(self, name, obj):
|
||||
"""Register an object in the object registry.
|
||||
|
||||
Prevents duplicated registrations and sets a slot to remove QObjects
|
||||
when they are destroyed.
|
||||
Sets a slot to remove QObjects when they are destroyed.
|
||||
"""
|
||||
if name in self.data:
|
||||
raise KeyError("Object '{}' is already registered ({})!".format(
|
||||
name, repr(self.data[name])))
|
||||
if isinstance(obj, QObject):
|
||||
obj.destroyed.connect(functools.partial(self.on_destroyed, name))
|
||||
super().__setitem__(name, obj)
|
||||
|
@ -585,6 +585,18 @@ def get_object(name):
|
||||
return QCoreApplication.instance().registry[name]
|
||||
|
||||
|
||||
def register_object(name, obj):
|
||||
"""Helper function to register an object."""
|
||||
QCoreApplication.instance().registry[name] = obj
|
||||
def register_object(name, obj, update=False):
|
||||
"""Helper function to register an object.
|
||||
|
||||
Args:
|
||||
name: The name the object will be registered as.
|
||||
obj: The object to register.
|
||||
update: If True, allows to update an already registered object.
|
||||
"""
|
||||
registry = QCoreApplication.instance().registry
|
||||
if not update and name in registry:
|
||||
raise KeyError("Object '{}' is already registered ({})!".format(
|
||||
name, repr(registry[name])))
|
||||
registry[name] = obj
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user