def itemData( self, index: QModelIndex, roles: Sequence[int] ) -> Mapping[int, Any]: """ Return item data from `index` for `roles`. The returned mapping is a read only view of *all* data roles accessed for the index through this caching interface. It will contain at least data for `roles`, but can also contain other ones. """ model = index.model() if model is not self.__model: self.setModel(model) # NOTE: QPersistentModelIndex's hash changes when it is invalidated; # it must be purged from __cache_data before that (`__connect_helper`) key = QPersistentModelIndex(index) try: item = self.__cache_data[key] except KeyError: data = item_data(index, roles) view = MappingProxy(data) self.__cache_data[key] = data, view else: data, view = item queryroles = tuple(filterfalse(data.__contains__, roles)) if queryroles: data.update(item_data(index, queryroles)) return view
def item_data( index: QModelIndex, roles: Sequence[int] ) -> Dict[int, Any]: """Query `index` for all `roles` and return them as a mapping""" model = index.model() datagetter = partial(model.data, index) values = map(datagetter, roles) return dict(zip(roles, values))
def createEditor(self, parent, options, index: QModelIndex): model = index.model() # type: EditableTableModel if not model.is_discrete(index.column()): return super().createEditor(parent, options, index) vals = model.discrete_vals(index.column()) edit = QLineEdit(parent) edit.setCompleter(QCompleter(vals, edit, filterMode=Qt.MatchContains)) def save(): if edit.text(): model.setData(index, edit.text()) edit.editingFinished.connect(save) return edit
def data(self, index: QModelIndex, role: int) -> Any: """Return item data for `index` and `role`""" model = index.model() if model is not self.__model: self.setModel(model) key = QPersistentModelIndex(index) try: item = self.__cache_data[key] except KeyError: data = item_data(index, (role,)) view = MappingProxy(data) self.__cache_data[key] = data, view else: data, view = item if role not in data: data[role] = model.data(index, role) return data[role]
def setEditorData(self, editor: VariableEditor, index: QModelIndex): editor.value = index.model().data(index, ValueRole)