rust 无法移出'Fn'闭包中的捕获变量

unhi4e5o  于 2022-12-29  发布在  其他
关注(0)|答案(1)|浏览(129)

我在我的gkt-rs代码中得到这些错误消息:

error[E0507]: cannot move out of `image`, a captured variable in an `Fn` closure
error[E0382]: borrow of moved value: `window`

我读过this questionthis question,但不知道如何将其应用于我的问题。我假设答案涉及RefCell,但无法正确调用。
这是我的代码:

use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Box, Button, FileChooserDialog, Image};
const APP_ID: &str = "org.gtk_rs.test";

fn main() {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_activate(build_ui);
    app.run();
}

fn build_ui(app: &Application) {
    let image = Image::builder().build();
    let button = Button::builder().label("Press me!").build();
    let mbox = Box::builder().build();
    mbox.append(&button);
    mbox.append(&image);
    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .child(&mbox)
        .build();
    button.connect_clicked(move |_| {
        let file_chooser = FileChooserDialog::new(
            Some("Open image"),
            Some(&window),
            gtk::FileChooserAction::Open,
            &[
                ("_Cancel", gtk::ResponseType::Cancel),
                ("_Open", gtk::ResponseType::Accept),
            ],      );
            file_chooser.connect_response(move |file_chooser, response| {
                if response == gtk::ResponseType::Accept {
                    let file = file_chooser.file().expect("Couldn't get file");
                    let filename = file.path().expect("Couldn't get file path");
                    image.set_from_file(Some(filename));
                }
                file_chooser.destroy();
            });
        file_chooser.show();
    });
    window.present();
}

我将非常感谢你的任何建议。

rggaifut

rggaifut1#

感谢@Masklinn,text_viewer example找到了答案:

use glib_macros::clone;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Box, Button, FileChooserDialog, Image};
const APP_ID: &str = "org.gtk_rs.test";

fn main() {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_activate(build_ui);
    app.run();
}

fn build_ui(app: &Application) {
    let image = Image::builder().build();
    let button = Button::builder().label("Press me!").build();
    let mbox = Box::builder().build();
    mbox.append(&button);
    mbox.append(&image);
    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .child(&mbox)
        .build();
    button.connect_clicked(clone!(@weak window => move |_| {
        let file_chooser = FileChooserDialog::new(
            Some("Open image"),
            Some(&window),
            gtk::FileChooserAction::Open,
            &[
                ("_Cancel", gtk::ResponseType::Cancel),
                ("_Open", gtk::ResponseType::Accept),
            ],      );
            file_chooser.connect_response(clone!(@weak image => move |file_chooser, response| {
                if response == gtk::ResponseType::Accept {
                    let file = file_chooser.file().expect("Couldn't get file");
                    let filename = file.path().expect("Couldn't get file path");
                    image.set_from_file(Some(filename));
                }
                file_chooser.destroy();
            }));
        file_chooser.show();
    }));
    window.present();
}

相关问题