pub trait QListModel {
type Item: QModelItem + Default + Clone;
// Required methods
fn len(&self) -> usize;
fn get(&self, index: usize) -> Option<&Self::Item>;
// Provided methods
fn set_unnotified(&mut self, _index: usize, _value: Self::Item) -> bool { ... }
fn push_unnotified(&mut self, value: Self::Item) { ... }
fn insert_unnotified(&mut self, _index: usize, _value: Self::Item) { ... }
fn pop_unnotified(&mut self) -> Option<Self::Item> { ... }
fn remove_unnotified(&mut self, _index: usize) -> Self::Item { ... }
fn reset_unnotified(&mut self) { ... }
}Expand description
A trait representing a list-based Qt model.
QListModel provides an interface for list-like data structures
that are exposed to Qt through the Model-View concept.
https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html.
This trait requires the qobject macro to set up the correct Qt proxy.
The macro will further generate functionality in the form of the
QListModelBase trait that supplements the QListModel functionality.
§Design
- The model owns items of associated type
Itemthat has to implement theQModelItemtrait. Roles are derived from theQModelItemimplementation. - Mutation methods are provided in an unnotified form, meaning they modify the underlying data without emitting Qt model signals.
- These methods are used by the automatically implemented
QListModelBasetrait to create methods that collaborate the UI about changes in collections.
As a minimum you have to implement the methods QListModel::len and
QListModel::get to create a readable list model. Further methods can be
implemented to make the model fully mutable.
Methods that do not return an Option or a boolean value must succeed
and perform exactly the operation described in the documentation to avoid
invalidating the synchronization between any views and the underlying data.
No additional structural changes may occur outside the provided functions.
Note, that default implementations may panic! if the corresponding method is
not overridden. It is your responsibility to make sure that these functions are
not called from QML.
§Example
use qtbridge::qobject;
#[qobject(Base = QListModel)]
mod backend {
use qtbridge::{QListModel, QListModelBase};
#[derive(Default)]
pub struct Backend {
string_list: Vec<String>,
}
impl QListModel for Backend {
type Item = String;
fn len(&self) -> usize {
self.string_list.len()
}
fn get(&self, index: usize) -> Option<&Self::Item> {
self.string_list.get(index)
}
}
}
The list model can be used in QML views as follows
ListView {
model: backend
delegate: Text {
required property string value
text: value
}
}Required Associated Types§
Sourcetype Item: QModelItem + Default + Clone
type Item: QModelItem + Default + Clone
The item type stored in the model.
Items must:
- Implement
QModelItemto integrate with Qt - Be
Defaultfor creating new items - Be
Clonefor safe data access and copying
Required Methods§
Provided Methods§
Sourcefn set_unnotified(&mut self, _index: usize, _value: Self::Item) -> bool
fn set_unnotified(&mut self, _index: usize, _value: Self::Item) -> bool
Sets the item at index. Reimplement this function but call
QListModelBase::set to notify Qt about the modification.
Returns true if the value was successfully set, or false if the
operation failed (e.g., index out of bounds or value fails
validation by the business logic).
The default implementation does nothing and returns false.
Sourcefn push_unnotified(&mut self, value: Self::Item)
fn push_unnotified(&mut self, value: Self::Item)
Appends an item to the end of the model. Reimplement this
function but call QListModelBase::push to notify Qt about the
modification.
The function has to accept the value. Validation has to be done before this function is called.
The default implementation falls back to QListModel::insert_unnotified,
which in turn panics by default.
Sourcefn insert_unnotified(&mut self, _index: usize, _value: Self::Item)
fn insert_unnotified(&mut self, _index: usize, _value: Self::Item)
Inserts value at index. Reimplement this function but
call QListModelBase::insert to notify Qt about the
modification.
The function has to accept the value. Validation has to be done before this function is called.
Panics by default. Implementors must override this method to support insertion.
Sourcefn pop_unnotified(&mut self) -> Option<Self::Item>
fn pop_unnotified(&mut self) -> Option<Self::Item>
Removes and returns the last item in the model. Reimplement this
function but call QListModelBase::pop to notify Qt
about the modification.
Returns None if the model is empty. If the model is not empty,
the function has to guarantee the success of the operation.
The default implementation falls back to QListModel::remove_unnotified,
which in turn panics by default.
Sourcefn remove_unnotified(&mut self, _index: usize) -> Self::Item
fn remove_unnotified(&mut self, _index: usize) -> Self::Item
Removes and returns the item at index. Reimplement this
function but call QListModelBase::remove to notify Qt
about the modification.
The index must be valid and the model has to guarantee the success of the operation.
Panics by default. Implementors must override this method to support removal.
Sourcefn reset_unnotified(&mut self)
fn reset_unnotified(&mut self)
Resets the model’s internal storage. Reimplement this function but
call QListModelBase::reset to notify Qt about the modification.
Panics by default. Implementors must override this method to support a model reset.
After QListModel::reset_unnotified returns, the internal storage
must reflect the new model state: QListModel::len and
QListModel::get must be consistent with the updated storage.