rustgtk3gladegdkpixbufgtk-rs

Rust Gtk set GtkImage from pixbuf using glade file


I'm trying to load an image from a scaled pixbuf to an existing GtkImage widget from Glade with Rust. I've read all the Gtk-rs documentations regarding this topic, and it should work based on that. However there is always a problem when trying to load from pixbuf. Here's my code to reproduce the error:

extern crate gtk;
extern crate gio;
extern crate gdk_pixbuf;

use gtk::prelude::*;
use gio::prelude::*;

use gtk::{Builder, MessageDialog, Window, ButtonsType, DialogFlags, MessageType, ResponseType, Image, ImageExt};
use gdk_pixbuf::{Pixbuf};

use std::env::args;

fn build_ui(application: &gtk::Application) {
    let glade_src = include_str!("test.glade");
    let builder = Builder::new_from_string(glade_src);

    let window: Window = builder.get_object("main").expect("Couldn't get main");
    let img2: Image = builder.get_object("img2").expect("Cangt get img2");
    window.set_application(Some(application));
    window.set_title("HLearn - 2020 - TatyĆ³");

    window.connect_delete_event(move |_, _| {
        println!("Hey");
        let window2: Window = builder.get_object("main").expect("Cant get 2nd");
        let dialog = MessageDialog::new(Some(&window2),
            DialogFlags::MODAL,
            MessageType::Question,
            ButtonsType::YesNo,
            "Are you sure to exit");
        dialog.set_title("Attention!");
        dialog.set_position(gtk::WindowPosition::__Unknown(0));
        let res = dialog.run();
        if res == ResponseType::Yes {
            println!("Yes");
            dialog.destroy();
            Inhibit(false)
        } else {
            println!("No");
            dialog.destroy();
            Inhibit(true)
        }
    });

    let builder2 = Builder::new_from_string(glade_src);
    let window3: Window = builder2.get_object("main").expect("Cant get 3rd");
    let (sx, sy) = window3.get_size();
    let pixbuf = Pixbuf::new_from_file_at_scale(
        "/home/daniel/RUST/test/src/test.gif",
        600,
        700,
        false);
    img2.set_from_pixbuf(Some(&pixbuf));

    window.show_all();
}

fn main() {
    let application = gtk::Application::new(
        Some("com.github.swanux.hlearn"),
        Default::default(),
    )
    .expect("Initialization failed...");

    application.connect_activate(|app| {
        build_ui(app);
    });

    application.run(&args().collect::<Vec<_>>());
}

And here's the glade file test.glade

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="main">
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkImage" id="img2">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="hexpand">True</property>
        <property name="vexpand">True</property>
        <property name="stock">gtk-missing-image</property>
      </object>
    </child>
  </object>
</interface>

When I try to load image from pixbuf, I always get this error message: expected struct gdk_pixbuf::Pixbuf, found enum std::result::Result. I'm using Linux with the newest Rust installed.


Solution

  • Here's the solution. Change this part :

    let pixbuf = Pixbuf::new_from_file_at_scale(
            "/home/daniel/RUST/test/src/test.gif",
            600,
            700,
            false); 
    

    To

    let pixbuf = Pixbuf::new_from_file_at_scale(
            "/home/daniel/RUST/test/src/test.gif",
            600,
            700,
            false).expect("Can't get pixbuf");