Replies: 2 comments 4 replies
-
you could set it up like this. assuming the following situation: export struct SingleCharInputField {
value: string
} and then inside your component. in property <[SingleCharInputField]> char-fields;
for field in char-fields : Text {
text: field.value;
} when (any) of the char-fields are altered from the code side, the field(s) will be rerendered accordingly. |
Beta Was this translation helpful? Give feedback.
-
single-char-field.slint export struct SingleCharInputField {
id: int,
value: string
} app-window.slint import { SingleCharInputField } from "single-char-field.slint";
export component AppWindow inherits Window {
width: 640px;
height: 480px;
in-out property <[SingleCharInputField]> char-fields: [
{ id: 0, value: "A" }, { id: 1, value: "B" }, { id: 2, value: "C" },
{ id: 3, value: "D" }, { id: 4, value: "E" }, { id: 5, value: "F" },
{ id: 6, value: "G" }, { id: 7, value: "H" }, { id: 8, value: "I" }
];
callback send-key-to-rust(id: int, value: string);
VerticalLayout {
padding: 10px;
spacing: 10px;
for field in char-fields : LineEdit {
text: field.value;
key-pressed(event) => {
let nav_key = event.text == Key.Backspace ||
event.text == Key.LeftArrow ||
event.text == Key.RightArrow ||
event.text == Key.Home ||
event.text == Key.End ||
event.text == Key.Delete;
if self.text.character-count > 0 && !nav-key { return accept; }
field.value = event.text;
send-key-to-rust(field.id, field.value);
return reject;
}
}
}
} main.rs #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::error::Error;
use slint::include_modules;
include_modules!();
fn main() -> Result<(), Box<dyn Error>> {
let ui = AppWindow::new()?;
ui.on_send_key_to_rust(|id, value| {
println!("id: {} - value: {}", id, value);
});
ui.run()?;
Ok(())
} now depending on what you want to do with the field(s), if you're using rust, then there's the ui.get_char_fields and ui.set_char_fields functions that you can use to read / write these properties. i've also thrown in the use of the key-pressed callback exposed by the line edit control, i've coupled this with a callback that basically sends the key over to the rust side. i'm also quite new at the slint / rust thing, so this is the best i can do with my current knowledge, there could be people out there having better advice on to handle things than what i'm showing at the moment. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to address an array of items that are created like this:
for i in [0, 1, 2, 3, 4] : SingleCharInputField { ... }
At some point later in my code I want to do two things: fetch the contents of the text property and clear it to "". I can't seem to figure out how to address those SingleChar components to get at their text fields.
Beta Was this translation helpful? Give feedback.
All reactions