Optimize ListModel setProperty() and update()
Avoid emiting dataChanged signals when nothing actually changed
This commit is contained in:
parent
4367f52ce5
commit
df088a8daa
@ -1,7 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import (
|
from typing import (
|
||||||
Any, Callable, Dict, Iterable, List, Mapping, MutableSequence, Optional,
|
Any, Callable, Dict, Iterable, List, Mapping, MutableSequence, Optional,
|
||||||
Sequence, Tuple, Union
|
Sequence, Set, Tuple, Union
|
||||||
)
|
)
|
||||||
|
|
||||||
from PyQt5.QtCore import (
|
from PyQt5.QtCore import (
|
||||||
@ -217,18 +217,26 @@ class ListModel(QAbstractListModel):
|
|||||||
|
|
||||||
to_update = self[i_index]
|
to_update = self[i_index]
|
||||||
|
|
||||||
for role in self.roles:
|
updated_roles: Set[int] = set()
|
||||||
if role not in ignore_roles:
|
|
||||||
try:
|
for role_name, role_num in self.roleNumbers().items():
|
||||||
setattr(to_update, role, getattr(value, role))
|
if role_name not in ignore_roles:
|
||||||
except AttributeError: # constant/not settable
|
old_value = getattr(to_update, role_name)
|
||||||
pass
|
new_value = getattr(value, role_name)
|
||||||
|
|
||||||
|
if old_value != new_value:
|
||||||
|
try:
|
||||||
|
setattr(to_update, role_name, new_value)
|
||||||
|
except AttributeError: # constant/not settable
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
updated_roles.add(role_num)
|
||||||
|
|
||||||
|
if updated_roles:
|
||||||
|
qidx = QAbstractListModel.index(self, i_index, 0)
|
||||||
|
self.dataChanged.emit(qidx, qidx, updated_roles)
|
||||||
|
self.changed.emit()
|
||||||
|
|
||||||
qidx = QAbstractListModel.index(self, i_index, 0)
|
|
||||||
updated = [number for name, number in self.roleNumbers().items()
|
|
||||||
if name not in ignore_roles]
|
|
||||||
self.dataChanged.emit(qidx, qidx, updated)
|
|
||||||
self.changed.emit()
|
|
||||||
return i_index
|
return i_index
|
||||||
|
|
||||||
|
|
||||||
@ -270,10 +278,11 @@ class ListModel(QAbstractListModel):
|
|||||||
i_index: int = self.indexWhere(self.mainKey, index) \
|
i_index: int = self.indexWhere(self.mainKey, index) \
|
||||||
if isinstance(index, str) else index
|
if isinstance(index, str) else index
|
||||||
|
|
||||||
setattr(self[i_index], prop, value)
|
if getattr(self[i_index], prop) != value:
|
||||||
qidx = QAbstractListModel.index(self, i_index, 0)
|
setattr(self[i_index], prop, value)
|
||||||
self.dataChanged.emit(qidx, qidx, (self.roleNumbers()[prop],))
|
qidx = QAbstractListModel.index(self, i_index, 0)
|
||||||
self.changed.emit()
|
self.dataChanged.emit(qidx, qidx, (self.roleNumbers()[prop],))
|
||||||
|
self.changed.emit()
|
||||||
|
|
||||||
|
|
||||||
@pyqtSlot(int, int)
|
@pyqtSlot(int, int)
|
||||||
|
Loading…
Reference in New Issue
Block a user