diff --git a/qmetaobject/src/listmodel.rs b/qmetaobject/src/listmodel.rs index dc6d88f..f096088 100644 --- a/qmetaobject/src/listmodel.rs +++ b/qmetaobject/src/listmodel.rs @@ -18,7 +18,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. use std::collections::HashMap; use std::iter::FromIterator; -use std::ops::Index; +use std::ops::{ Index, IndexMut }; use cpp::cpp; @@ -274,18 +274,25 @@ impl SimpleListModel { } pub fn change_line(&mut self, index: usize, value: T) { self.values[index] = value; - let idx = (self as &mut dyn QAbstractListModel).row_index(index as i32); - (self as &mut dyn QAbstractListModel).data_changed(idx, idx); + self.data_changed(index); } pub fn reset_data(&mut self, data: Vec) { (self as &mut dyn QAbstractListModel).begin_reset_model(); self.values = data; (self as &mut dyn QAbstractListModel).end_reset_model(); } + pub fn data_changed(&mut self, index: usize) { + let idx = (self as &mut dyn QAbstractListModel).row_index(index as i32); + (self as &mut dyn QAbstractListModel).data_changed(idx, idx); + } /// Returns an iterator over the items in the model pub fn iter(&self) -> impl Iterator { self.values.iter() } + /// Returns mutable iterator over the items in the model + pub fn iter_mut(&mut self) -> impl Iterator { + self.values.iter_mut() + } } impl FromIterator for SimpleListModel @@ -319,3 +326,12 @@ where &self.values[index] } } + +impl IndexMut for SimpleListModel +where + T: SimpleListItem, +{ + fn index_mut(&mut self, index: usize) -> &mut T { + &mut self.values[index] + } +}