disponibles dans le projet biblioapp-composant .
from PyQt5.QtCore import pyqtSlot, pyqtProperty, QAbstractListModel,
QModelIndex, Qt
class FileStringListModel(QAbstractListModel):
def __init__(self, data, parent=None, *args):
QAbstractListModel.__init__(self, parent, *args)
self._data = data
self._file = None
self._just_created = False
def _file_write(self):
if self._file is None:
return False
with open(self._file, 'w') as f:
json.dump(self._data, f)
return True
# …
# API d'un modèle PyQt.
def setData(self, index, value, role):
if self._file is None or not index.isValid():
return False
self._data[index.row()] = value
self.dataChanged.emit(index, index, [role])
self._file_write()
return True
def insertRows(self, row, count, parent=None):
if self._file is None:
return False
super(QAbstractListModel,
self).beginInsertRows(QModelIndex(), row, row + count - 1)
for i in range(count):
self._data.insert(row + i, None)
super(QAbstractListModel, self).endInsertRows()
self.countChanged.emit()
return True
# …
# API d'un modèle Qt Quick.
countChanged = pyqtSignal()
@pyqtProperty(int, notify=countChanged)
def count(self):
387
Précédent

- 388/494

Suivant