add callback to add row to slint ui

This commit is contained in:
2026-05-14 21:38:20 +02:00
parent 175f5459a8
commit 922c28d426
2 changed files with 31 additions and 13 deletions
+19 -1
View File
@@ -1,7 +1,9 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::error::Error;
mod rutracker; mod rutracker;
use slint::{ModelRc, StandardListViewItem, VecModel};
use std::error::Error;
use std::rc::Rc;
slint::include_modules!(); slint::include_modules!();
@@ -13,6 +15,22 @@ fn main() -> Result<(), Box<dyn Error>> {
let _response = rutracker::search(&text); let _response = rutracker::search(&text);
}); });
let table_vec: Vec<ModelRc<StandardListViewItem>> = vec![];
let table_model = Rc::new(VecModel::from(table_vec));
let test = "test";
ui.set_table_data(table_model.to_owned().into());
ui.on_add_row({
move || {
table_model.push(VecModel::from_slice(&[
StandardListViewItem::from(slint::SharedString::from(test)),
//StandardListViewItem::from("<new>"),
]));
}
});
ui.run().unwrap();
ui.run()?; ui.run()?;
Ok(()) Ok(())
+12 -12
View File
@@ -6,6 +6,9 @@ export component AppWindow inherits Window {
in-out property <string> search-text: ""; in-out property <string> search-text: "";
callback request-text-input(string); callback request-text-input(string);
in-out property <[[StandardListViewItem]]> table-data;
callback add-row();
VerticalBox { VerticalBox {
alignment: stretch; alignment: stretch;
// width: 50%; // width: 50%;
@@ -20,7 +23,7 @@ export component AppWindow inherits Window {
} }
HorizontalBox { HorizontalBox {
StandardTableView { table := StandardTableView {
// width: 230px; // width: 230px;
// height: 200px; // height: 200px;
@@ -30,17 +33,14 @@ export component AppWindow inherits Window {
{ title: "Seeders" }, { title: "Seeders" },
{ title: "Leechers" }, { title: "Leechers" },
]; ];
rows: [ rows: table-data;
[ }
{ text: "Item 1" }, { text: "Item 2" },
], Button {
[ text: "Add row";
{ text: "Item 1" }, { text: "Item 2" }, clicked => {
], add-row();
[ }
{ text: "Item 1" }, { text: "Item 2" },
]
];
} }
} }
} }