[英]Cannot move out a captured variable in an `Fn` closure
I'm getting these error messages in my gkt-rs code:我在 gkt-rs 代码中收到以下错误消息:
error[E0507]: cannot move out of `image`, a captured variable in an `Fn` closure
error[E0382]: borrow of moved value: `window`
I've read this question and this question , but don't see how to apply it to my problem.我已经阅读了这个问题和这个问题,但不知道如何将其应用于我的问题。 I assume the answer involves RefCell, but can't get the invocation right.
我假设答案涉及 RefCell,但无法正确调用。
This is my code:这是我的代码:
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();
}
I'd be grateful for any suggestions.如果有任何建议,我将不胜感激。
Thanks @Masklinn, the text_viewer example had the answer:感谢@Masklinn, text_viewer 示例给出了答案:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.