pub struct ModelRc<T>(/* private fields */);
Expand description
ModelRc is a type wrapper for a reference counted implementation of the Model
trait.
Models are used to represent sequences of the same data type. In .slint
code those
are represented using the [T]
array syntax and typically used in for
expressions,
array properties, and array struct fields.
For example, a property <[string]> foo
will be of type ModelRc<SharedString>
and, behind the scenes, wraps a Rc<dyn Model<Data = SharedString>>.
An array struct field will also be of type ModelRc
:
export struct AddressBook {
names: [string]
}
When accessing AddressBook
from Rust, the names
field will be of type ModelRc<SharedString>
.
There are several ways of constructing a ModelRc in Rust:
- An empty ModelRc can be constructed with
ModelRc::default()
. - A
ModelRc
can be constructed from a slice or an array using theFrom
trait. This allocates aVecModel
. - Use
ModelRc::new()
to construct aModelRc
from a type that implements theModel
trait, such asVecModel
or your own implementation. - If you have your model already in an
Rc
, then you can use theFrom
trait to convert fromRc<dyn Model<Data = T>>
toModelRc
.
§Example
use slint::{slint, SharedString, ModelRc, Model, VecModel};
use std::rc::Rc;
slint!{
import { Button } from "std-widgets.slint";
export component Example {
callback add_item <=> btn.clicked;
in property <[string]> the_model;
HorizontalLayout {
for it in the_model : Text { text: it; }
btn := Button { text: "Add"; }
}
}
}
let ui = Example::new().unwrap();
// Create a VecModel and put it in an Rc.
let the_model : Rc<VecModel<SharedString>> =
Rc::new(VecModel::from(vec!["Hello".into(), "World".into()]));
// Convert it to a ModelRc.
let the_model_rc = ModelRc::from(the_model.clone());
// Pass the model to the ui: The generated set_the_model setter from the
// the_model property takes a ModelRc.
ui.set_the_model(the_model_rc);
// We have kept a strong reference to the_model, to modify it in a callback.
ui.on_add_item(move || {
// Use VecModel API: VecModel uses the Model notification mechanism to let Slint
// know it needs to refresh the UI.
the_model.push("SomeValue".into());
});
// Alternative: we can re-use a getter.
let ui_weak = ui.as_weak();
ui.on_add_item(move || {
let ui = ui_weak.unwrap();
let the_model_rc = ui.get_the_model();
let the_model = the_model_rc.as_any().downcast_ref::<VecModel<SharedString>>()
.expect("We know we set a VecModel earlier");
the_model.push("An Item".into());
});
Implementations§
Trait Implementations§
Source§impl<T> Model for ModelRc<T>
impl<T> Model for ModelRc<T>
Source§fn row_data(&self, row: usize) -> Option<Self::Data>
fn row_data(&self, row: usize) -> Option<Self::Data>
Returns the data for a particular row. Read more
Source§fn set_row_data(&self, row: usize, data: Self::Data)
fn set_row_data(&self, row: usize, data: Self::Data)
Sets the data for a particular row. Read more
Source§fn model_tracker(&self) -> &dyn ModelTracker
fn model_tracker(&self) -> &dyn ModelTracker
The implementation should return a reference to its
ModelNotify
field. Read moreAuto Trait Implementations§
impl<T> Freeze for ModelRc<T>
impl<T> !RefUnwindSafe for ModelRc<T>
impl<T> !Send for ModelRc<T>
impl<T> !Sync for ModelRc<T>
impl<T> Unpin for ModelRc<T>
impl<T> !UnwindSafe for ModelRc<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ModelExt for Twhere
T: Model,
impl<T> ModelExt for Twhere
T: Model,
Source§fn row_data_tracked(&self, row: usize) -> Option<Self::Data>
fn row_data_tracked(&self, row: usize) -> Option<Self::Data>
Convenience function that calls
ModelTracker::track_row_data_changes
before returning Model::row_data
. Read moreSource§fn map<F, U>(self, map_function: F) -> MapModel<Self, F>
fn map<F, U>(self, map_function: F) -> MapModel<Self, F>
Returns a new Model where all elements are mapped by the function
map_function
.
This is a shortcut for MapModel::new()
.Source§fn filter<F>(self, filter_function: F) -> FilterModel<Self, F>
fn filter<F>(self, filter_function: F) -> FilterModel<Self, F>
Returns a new Model where the elements are filtered by the function
filter_function
.
This is a shortcut for FilterModel::new()
.Source§fn sort(self) -> SortModel<Self, AscendingSortHelper>
fn sort(self) -> SortModel<Self, AscendingSortHelper>
Returns a new Model where the elements are sorted ascending.
This is a shortcut for
SortModel::new_ascending()
.Source§fn sort_by<F>(self, sort_function: F) -> SortModel<Self, F>
fn sort_by<F>(self, sort_function: F) -> SortModel<Self, F>
Returns a new Model where the elements are sorted by the function
sort_function
.
This is a shortcut for SortModel::new()
.Source§fn reverse(self) -> ReverseModel<Self>where
Self: Sized + 'static,
fn reverse(self) -> ReverseModel<Self>where
Self: Sized + 'static,
Returns a new Model where the elements are reversed.
This is a shortcut for
ReverseModel::new()
.