You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust开发Slint GUI时,如何正确获取TextEdit的最新文本?

问题:Slint中无法获取TextEdit修改后的文本值

我正在学习Slint和Rust,读取TextEdit组件的文本属性时遇到问题:点击Gen QRNote按钮,初始文本“Pouet”能正常打印,但修改TextEdit内容后再次点击,输出的还是初始值,无法获取编辑后的内容。

我的Slint GUI定义:

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();
            }
        }
    }
}

Rust的main()函数代码:

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()
}

解决方法

核心问题不是UI引用的问题(你的as_weak()用法是正确的),而是属性绑定的方向错误:

  1. 你定义的thetext是out属性,这意味着它只能从组件内部向外传递值,无法接收外部(或TextEdit)的修改。需要改成in-out属性,支持双向传递。
  2. 原代码中TextEdit的text只是单向绑定到thetext,TextEdit内容修改后不会同步回thetext,所以你读取的始终是初始值。

有两种修复方式:

方式一:手动同步文本值

修改Slint代码:

  • 将out property<string> thetext: "Pouet";改为in-out property<string> thetext: "Pouet";
  • 在TextEdit的edited回调中,把当前文本同步到thetext属性:
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();
    VerticalBox {
        TextEdit {
            text: thetext;
            width: 300px;
            height: 300px;
            edited => {
                root.thetext = self.text;
                root.qr-note-edited();
            }
        }
        Button {
            text: "Increase value";
            clicked => {
                root.request-increase-value();
            }
        }
        Button {
            text: "Gen QRNote";
            clicked => {
                root.compute-qr-code();
            }
        }
    }
}

方式二:使用双向绑定(更简洁)

Slint支持双向绑定语法<=>,直接让TextEdit的text和thetext属性双向同步,无需手动在回调里赋值:

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();
    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();
            }
        }
    }
}

两种方式修改后,点击Gen QRNote按钮就能正确获取TextEdit中编辑后的文本值了,Rust代码不需要做任何修改。


内容的提问来源于stack exchange,提问作者FabienM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 06:43:17