moment/harmonyqml/backend/model/list_model_map.py
miruka 5c8fd4500d Fix garbage collection of ListModelMap items
Prevent ListModel items from being deleted on the C++ side (specially
when using .get() from QML and a new ListModel is created)
by setting their parent to the ListModelMap.
2019-04-13 09:59:34 -04:00

41 lines
882 B
Python

from typing import Any, DefaultDict
from PyQt5.QtCore import QObject, pyqtSlot
from .list_model import ListModel
class ListModelMap(QObject):
def __init__(self) -> None:
super().__init__()
# Set the parent to prevent item garbage-collection on the C++ side
self.dict: DefaultDict[Any, ListModel] = \
DefaultDict(lambda: ListModel(parent=self))
@pyqtSlot(str, result="QVariant")
def get(self, key) -> ListModel:
return self.dict[key]
def __getitem__(self, key) -> ListModel:
return self.dict[key]
def __setitem__(self, key, value: ListModel) -> None:
value.setParent(self)
self.dict[key] = value
def __detitem__(self, key) -> None:
del self.dict[key]
def __iter__(self):
return iter(self.dict)
def __len__(self) -> int:
return len(self.dict)