pub struct ModelRc<T>(_);
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ยง
ยงimpl<T> Model for ModelRc<T>
impl<T> Model for ModelRc<T>
ยงfn row_data(&self, row: usize) -> Option<<ModelRc<T> as Model>::Data>
fn row_data(&self, row: usize) -> Option<<ModelRc<T> as Model>::Data>
Returns the data for a particular row. This function should be called with
row < row_count()
. Read moreยงfn set_row_data(&self, row: usize, data: <ModelRc<T> as Model>::Data)
fn set_row_data(&self, row: usize, data: <ModelRc<T> as Model>::Data)
Sets the data for a particular row. Read more
ยง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> !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
ยงimpl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere T: Any,
ยงfn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
Convert
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.ยงfn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.ยงfn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
โs vtable from &Trait
โs.ยงfn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
โs vtable from &mut Trait
โs.sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
sourceยงfn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourceยงfn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
ยงimpl<T> ModelExt for Twhere
T: Model,
impl<T> ModelExt for Twhere T: Model,
ยง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 moreยงfn map<F, U>(self, map_function: F) -> MapModel<Self, F>where
Self: Sized + 'static,
F: Fn(Self::Data) -> U + 'static,
fn map<F, U>(self, map_function: F) -> MapModel<Self, F>where Self: Sized + 'static, F: Fn(Self::Data) -> U + 'static,
Returns a new Model where all elements are mapped by the function
map_function
.
This is a shortcut for MapModel::new()
.ยงfn filter<F>(self, filter_function: F) -> FilterModel<Self, F>where
Self: Sized + 'static,
F: Fn(&Self::Data) -> bool + 'static,
fn filter<F>(self, filter_function: F) -> FilterModel<Self, F>where Self: Sized + 'static, F: Fn(&Self::Data) -> bool + 'static,
Returns a new Model where the elements are filtered by the function
filter_function
.
This is a shortcut for FilterModel::new()
.ยงimpl<T> NoneValue for Twhere
T: Default,
impl<T> NoneValue for Twhere T: Default,
type NoneType = T
ยงfn null_value() -> T
fn null_value() -> T
The none-equivalent value.
ยงimpl<T> Pointable for T
impl<T> Pointable for T
sourceยงimpl<R, P> ReadPrimitive<R> for Pwhere
R: Read + ReadEndian<P>,
P: Default,
impl<R, P> ReadPrimitive<R> for Pwhere R: Read + ReadEndian<P>, P: Default,
sourceยงfn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian()
.