0

I want to refresh a DrawingArea each time I call the draw() function. I have read Update drawing function of a DrawingArea, so I know that I need to disconnect the old drawing signal before connecting a new one. DrawingArea has the method disconnect(id: SignalHandlerId), so this code compiles, but no drawing is displayed:

let id = drawing_area.connect_draw(move |_, cr| {
    // drawing
});
drawing_area.disconnect(id);

When I try to save the id to disconnect the old signal before connecting the new one, I need to explicitly declare id as SignalHandlerId:

extern crate cairo;
extern crate gio;
extern crate gtk;

use gtk::prelude::*;

pub struct DrawingMechanism {
    id: gtk::glib::signal::SignalHandlerId,
    used: bool,
}

impl DrawingMechanism {
    pub fn draw(&self, drawing_area: &gtk::DrawingArea) {
        if self.used {
            drawing_area.disconnect(self.id);
        } else {
            self.used = true; // first call doesn`t have old id
        }

        self.id = drawing_area.connect_draw(move |_, cr| {
            // drawing
        });
    }
}

Here is the problem: the compiler tells me that gtk::glib is private, so I cannot use it. If I try to add the glib crate and use it instead of gtk::glib, the compiler tells me that I am using glib::SignalHandlerId, and what DrawingArea::connect_draw() returns is glib::signal::SignalHandlerId.

How do I declare the id variable so I can store the old signal id?

EDIT: It was just a version mismatch, I was trying to use glib version 0.9.0, but gtk was using 0.8.2.

  • Welcome to Stack Overflow! It looks like your question might be answered by the answers of [How do I print the type of a variable in Rust?](https://stackoverflow.com/q/21747136/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Feb 03 '20 at 21:29
  • TL;DR: have the compiler print out the type of `id` for you and then use that in your struct. – Shepmaster Feb 03 '20 at 21:29
  • Please paste the exact and entire error that you're getting — that will help us to understand what the problem is so we can help best. Sometimes trying to interpret an error message is tricky and it's actually a different part of the error message that's important. – Shepmaster Feb 03 '20 at 21:33
  • You may also be running into a version mismatch. Read and follow [Why is a trait not implemented for a type that clearly has it implemented?](https://stackoverflow.com/q/44437123/155423) to ensure your versions of `glib` match. – Shepmaster Feb 03 '20 at 22:01
  • You are right, thank you, it was a version mismatch. – Jackhalabardnik Feb 04 '20 at 18:12

0 Answers0