slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
17.55k stars 601 forks source link

Unable to Retrieve and Update Text in LineEdit and TextEdit Widgets #4794

Closed chen-gz closed 8 months ago

chen-gz commented 8 months ago

I have following code in .slint file

            LineEdit {
                text: root.data;
                // edited(s) => {
                //     root.data= s;
                // }
            }

The ui.set_data() works fine. But I can't not get the user inputs. If I add comment out lines, get_data works fine but set_data does not work any more.

lifeRobot commented 8 months ago

I have following code in .slint file

            LineEdit {
                text: root.data;
                // edited(s) => {
                //     root.data= s;
                // }
            }

The ui.set_data() works fine. But I can't not get the user inputs. If I add comment out lines, get_data works fine but set_data does not work any more.

Have you not set ·property data· to out, they may look like in-out property <string> data; or out property <string> data;, see slintpad

chen-gz commented 8 months ago

I have in-out property for data.

in-out property <string> data;

Here is the code that I used in a callback function.

                let data = ui.get_data().to_string();
                let data = data.as_bytes()
                    .iter()
                    .map(|&b| format!("{:02X}", b))
                    .collect::<Vec<String>>()
                    .join(" ");
                ui.set_data(slint::SharedString::from(data.clone()));   // with edited(s)   --- doesn;t work 
                ui.set_data1(slint::SharedString::from(data));            // without edited(s) --- works

main.rs


#![windows_subsystem = "windows"]
slint::include_modules!();

fn main() -> Result<(), slint::PlatformError> {
    let ui = AppWindow::new()?;
    ui.on_edit({
        let ui_handle = ui.as_weak();
        move || {
            let ui = ui_handle.upgrade().unwrap();
            let data = ui.get_data().to_string();
            // add 's' to the end of the data
            let data = format!("{}s", data);
            ui.set_data(slint::SharedString::from(data.clone()));
            ui.set_data1(slint::SharedString::from(data.clone()));
        }
    });

    ui.run()
}

.slint

export component AppWindow inherits Window{
    width: 800px;
    height: 600px;
    in-out property <string> data;
    in-out property <string> data1;
    callback edit();
    VerticalLayout {
        LineEdit {
            text: root.data;
            edited(s) => {
                root.data = s;
                debug(root.data);
                edit();
            }
        }
        LineEdit {
            text: root.data1;   

        }
    }
}

What I expect is when enter some character in first LineEdit. A 's' will append to the string and write back to LineEdits.

The content of two LineEdits should be same, but they are not.

image