0

I use gtk4-rs to build a gui program. the 1st parameter of the select_folder function shows an error: "the trait bound ObjectImplRef<imp::Window>: IsA<gtk4::Window> is not satisfied"

impl ObjectImpl for Window {
    fn constructed(&self) {
        self.parent_constructed();
        let window = self;
        self.file_button.connect_clicked(glib::clone!(
            #[weak]
            window,
            move|_|{
                let dialog = FileDialog::builder()
                .title("choose a directory")
                .accept_label("confirm")
                .modal(true)
                .build();
                dialog.select_folder(Some(&window), gio::Cancellable::NONE, |path|{
                    if let Ok(filepath) = path {
                        println!("{}", filepath.path().unwrap().to_string_lossy());
                    }
                });
            }
        ));
    }
}

but I checked many times , configurations are correct, anyone has encountered this? my configurations: mod imp;

use glib::Object;
use gtk::{gio, glib, Application};


glib::wrapper! {
    pub struct Window(ObjectSubclass<imp::Window>)
        @extends gtk::ApplicationWindow, gtk::Window, gtk::Widget,
        @implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
                    gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}

impl Window {
    pub fn new(app: &Application) -> Self {
        // Create new window
        Object::builder().property("application", app).build()
    }
}

imp.rs:

use gtk::glib::subclass::InitializingObject;
use gtk::{gio, prelude::*, FileDialog};
use gtk::subclass::prelude::*;
use gtk::{glib, Button, CompositeTemplate};


#[derive(CompositeTemplate, Default)]
#[template(resource = "/com/****/zsyn/window.ui")]       //path is right
pub struct Window {
    #[template_child]
    pub file_button: TemplateChild<Button>,
}

#[glib::object_subclass]
impl ObjectSubclass for Window {
    const NAME: &'static str = "MainWindow";
    type Type = super::Window;
    type ParentType = gtk::ApplicationWindow;

    fn class_init(klass: &mut Self::Class) {
        klass.bind_template();
        // klass.bind_template_callbacks();
    }

    fn instance_init(obj: &InitializingObject<Self>) {
        obj.init_template();
    }
}

impl ObjectImpl for Window {
    fn constructed(&self) {
        // Call "constructed" on parent
        self.parent_constructed();
        let window = self;
        self.file_button.connect_clicked(glib::clone!(
            #[weak]
            window,
            move|_|{
                let dialog = FileDialog::builder()
                .title("choose a directory")
                .accept_label("confirm")
                .modal(true)
                .build();
                dialog.select_folder(Some(&window), gio::Cancellable::NONE, |path|{
                    if let Ok(filepath) = path {
                        println!("{}", filepath.path().unwrap().to_string_lossy());
                    }
                });
            }
        ));
    }
}

// Trait shared by all widgets
impl WidgetImpl for Window {}

// Trait shared by all windows
impl WindowImpl for Window {}

// Trait shared by all application windows
impl ApplicationWindowImpl for Window {}

xml templates are also correct, because if I comment the callback of the button, it is all right.

1 Answer 1

0

It looks like you have to use window.obj() instead of window because window is the private struct and not the GObject.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.