You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How to modify the ModelRc for update Slint Ui in Rust? I try to update ui by modify the ModelRc in a thread with a timer, but I found the memory always increase when the application running.
I tried to verify in a simplified code, but the problem still persists。
way1 set with new ModelRc
way2 set data by row for ModelRc
way3 set data by row for VecModel
way4 remove and insert row for VecModel
ps: When I only comment out the set line of code(for ModelRc or VecModel), the memory will remain normal, but UI not update.
Rust code
use std::rc::Rc;
use std::{error::Error, thread};
use std::time::Duration;
use slint::{Model, ModelRc, SharedString, VecModel};
slint::include_modules!();
fn main() -> Result<(), Box<dyn Error>> {
let ui = AppWindow::new()?;
ui.on_request_increase_value({
let ui_handle = ui.as_weak();
move || {
let ui = ui_handle.unwrap();
ui.set_counter(ui.get_counter() + 1);
}
});
let ui_handle = ui.as_weak();
thread::spawn(move || {
let mut count = 0;
let ten_millis = Duration::from_millis(10);
loop {
count +=1;
let _ = ui_handle.upgrade_in_event_loop(move |handle| {
//way1 set with new ModelRc
// let mut models:Vec<LabelInfo> = Vec::new();
// for i in 0..3 {
// let name = format!("Lable Test {}: {}", i, count);
// let mut label: LabelInfo = LabelInfo::default();
// label.name = SharedString::from(name);
// models.push(label);
// }
// let the_model : Rc<VecModel<LabelInfo>> = Rc::new(VecModel::from(models));
// let the_model_rc = ModelRc::from(the_model.clone());
// handle.set_labels(the_model_rc);
//way2 set data by row for ModelRc
// let ui_labels_rc = handle.get_labels();
// for row in 0..ui_labels_rc.row_count() {
// let mut label = ui_labels_rc.row_data(row).unwrap();
// let name = "Lable Test One:".to_string() + &count.to_string();
// label.name = SharedString::from(name);
// // ui_labels_rc.set_row_data(row, label);
// }
//way3 set data by row for VecModel<LabelInfo>
// let ui_labels_rc = handle.get_labels();
// let the_model = ui_labels_rc.as_any().downcast_ref::<VecModel<LabelInfo>>().expect("We know we set a VecModel earlier");
// for row in 0..the_model.row_count() {
// let mut label = ui_labels_rc.row_data(row).unwrap();
// let name = "Lable Test One:".to_string() + &count.to_string();
// label.name = SharedString::from(name);
// the_model.set_row_data(row, label);
// }
//way4 remove and insert row for VecModel<LabelInfo>
let ui_labels_rc = handle.get_labels();
let the_model = ui_labels_rc.as_any().downcast_ref::<VecModel<LabelInfo>>().expect("We know we set a VecModel earlier");
for row in 0..the_model.row_count() {
let mut label = ui_labels_rc.row_data(row).unwrap();
let name = "Lable Test One:".to_string() + &count.to_string();
label.name = SharedString::from(name);
the_model.remove(row);
the_model.insert(row, label);
}
});
thread::sleep(ten_millis);
}
});
ui.run()?;
Ok(())
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
How to modify the ModelRc for update Slint Ui in Rust? I try to update ui by modify the ModelRc in a thread with a timer, but I found the memory always increase when the application running.
I tried to verify in a simplified code, but the problem still persists。
ps: When I only comment out the set line of code(for ModelRc or VecModel), the memory will remain normal, but UI not update.
Rust code
Slint code
Beta Was this translation helpful? Give feedback.
All reactions