In Rust, how to get the text of TextEdit widget Slint gui

I’m learning Slint (and Rust) and I’m stuck reading text attribute of TextEdit widget.

My slint gui is described as following :

import { Button, VerticalBox } from "std-widgets.slint";
import { HorizontalBox, TextEdit } from "std-widgets.slint";

export component AppWindow inherits Window {
    in-out property<int> counter: 42;
    out property<string> thetext: "Pouet";
    callback request-increase-value();
    callback compute-qr-code();
    callback qr-note-edited();
    VerticalBox {
        TextEdit {
            text: thetext;
            width: 300px;
            height: 300px;
            edited => {
                root.qr-note-edited();
            }
        }
        Button {
            text: "Increase value";
            clicked => {
                root.request-increase-value();
            }
        }
        Button {
            text: "Gen QRNote";
            clicked => {
                root.compute-qr-code();
            }
        }
    }
}

And, in the rust main() function I’m trying to get the text value to print it in console :

slint::include_modules!();

fn main() -> Result<(), slint::PlatformError> {
    let ui = AppWindow::new()?;

    let ui_handle = ui.as_weak();
    ui.on_request_increase_value(move || {
        let ui = ui_handle.unwrap();
        ui.set_counter(ui.get_counter() + 1);
        println!("button pushed");
    });

    let ui_handle = ui.as_weak();
    ui.on_compute_qr_code(move || {
        let ui = ui_handle.unwrap();
        println!("{}",ui.get_thetext());
    });

    let ui_handle = ui.as_weak();
    ui.on_qr_note_edited(move || {
        let ui = ui_handle.unwrap();
        println!("text changed -> {}", ui.get_thetext());
    });

    ui.run()
}

When I click on Gen QRNote button, initial text “Pouet” is displayed.
But if I modify text in gui TextEdit and re-click on Gen QRNote, edited text is not displayed, only the initial “Pouet” text.

It seems that ui is duplicated. What is the right method to point on initial ui reference ?

Alternative 1

You need to manually update the thetext property when the edited event occurs:

import { Button, VerticalBox } from "std-widgets.slint";
import { HorizontalBox, TextEdit } from "std-widgets.slint";

export component AppWindow inherits Window {
    in-out property<int> counter: 42;
    out property<string> thetext: "Pouet";
    callback request-increase-value();
    callback compute-qr-code();
    callback qr-note-edited();
    VerticalBox {
        TextEdit {
            text: thetext;
            width: 300px;
            height: 300px;
            edited(text) => {
                thetext = text;
                root.qr-note-edited();
            }
        }
        Button {
            text: "Increase value";
            clicked => {
                root.request-increase-value();
            }
        }
        Button {
            text: "Gen QRNote";
            clicked => {
                root.compute-qr-code();
            }
        }
    }
}

Alternative 2

You can make the thetext property in-out and accept a string argument in your qr-note-edited callback:

import { Button, VerticalBox } from "std-widgets.slint";
import { HorizontalBox, TextEdit } from "std-widgets.slint";

export component AppWindow inherits Window {
    in-out property<int> counter: 42;
    in-out property<string> thetext: "Pouet";
    callback request-increase-value();
    callback compute-qr-code();
    callback qr-note-edited(string);
    VerticalBox {
        TextEdit {
            text: thetext;
            width: 300px;
            height: 300px;
            edited(text) => {
                root.qr-note-edited(text);
            }
        }
        Button {
            text: "Increase value";
            clicked => {
                root.request-increase-value();
            }
        }
        Button {
            text: "Gen QRNote";
            clicked => {
                root.compute-qr-code();
            }
        }
    }
}

Then you can update the thetext property in your rust callback like so:

ui.on_qr_note_edited(move |new_text| {
    let ui = ui_handle.unwrap();
    ui.set_thetext(new_text);
    println!("text changed -> {}", ui.get_thetext());
});

Leave a Comment