Switch 1 sets Switch 2 ... Binding lost? #7108
Answered
by
mcitem
dirkeinecke
asked this question in
Q&A
Replies: 2 comments 9 replies
-
Is this the behavior you want? import { Switch } from "std-widgets.slint";
export component App inherits Window {
out property <bool> switch1 <=> switch_1.checked;
out property <bool> switch2 <=> switch_2.checked;
width: 100px;
height: 100px;
VerticalLayout {
switch_1 := Switch {
text: "Switch 1";
toggled() => {
switch_2.checked = self.checked;
}
}
switch_2 := Switch {
text: "Switch 2";
}
}
} No backend code is needed in that case ;-) |
Beta Was this translation helpful? Give feedback.
8 replies
-
use slint::Weak;
slint::slint! {
import { Switch } from "std-widgets.slint";
export component App inherits Window {
in property <bool> checked_1 <=> switch_1.checked;
in property <bool> checked_2 <=> switch_2.checked;
callback toggled_1 <=> switch_1.toggled;
callback toggled_2 <=> switch_2.toggled;
width: 100px;
height: 100px;
VerticalLayout {
switch_1 := Switch {
text: "Switch 1";
}
switch_2 := Switch {
text: "Switch 2";
}
}
}
}
fn main() {
let app: App = App::new().unwrap();
let weak_1: Weak<App> = app.as_weak();
app.on_toggled_1(move || {
if let Some(app) = weak_1.upgrade() {
let is_checked: bool = app.get_checked_1();
match is_checked {
true => {
// do something when #1 checked
println!("Switch 1 is checked");
app.set_checked_1(true);
}
false => {
app.set_checked_1(false);
}
}
app.set_checked_2(is_checked);
}
});
let weak_2: Weak<App> = app.as_weak();
app.on_toggled_2(move || {
if let Some(app) = weak_2.upgrade() {
let is_checked: bool = app.get_checked_2();
match is_checked {
true => {
app.set_checked_2(true);
}
false => {
app.set_checked_2(false);
}
}
}
});
app.run().unwrap();
} I'm not entirely sure what specific outcome you're looking for. Is it correct that when you set #1, #2 will also be in the same state? And switching #2 does not affect #1. If not, please provide more detailed background information. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
dirkeinecke
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi!
I want to control one switch (#1) with another switch (#2). At first glance, this seems to work fine. When I turn on "Switch 1", "Switch 2" also turns on, and vice versa.
However, when I use the GUI to click "Switch 2", turning it on and then off again, I can no longer control "Switch 2" with "Switch 1". This seems very strange. Or am I overlooking a catch?
Here is my source code for this:
Beta Was this translation helpful? Give feedback.
All reactions