As I’m slowly figuring out how to build an application with gtk-rs, I got stuck on the following: I implemented a custom GObject NewMeasurementWindow
which contains a spin button as a child for the user to input a number standing for a temperature.
use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::subclass::prelude::*;
use gtk::{glib, CompositeTemplate};
use gtk::{prelude::*, SpinButton};
// Object holding the state
#[derive(Properties, CompositeTemplate, Default)]
#[properties(wrapper_type = super::NewMeasurementWindow)]
#[template(resource = "/org/codeberg/jdw/Thermometer/NewMeasurementWindow.ui")]
pub struct NewMeasurementWindow {
#[template_child]
pub temperature_spinbutton: TemplateChild<SpinButton>,
}
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for NewMeasurementWindow {
// `NAME` needs to match `class` attribute of template
const NAME: &'static str = "ThermometerNewMeasurementWindow";
type Type = super::NewMeasurementWindow;
type ParentType = gtk::Window;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
// Trait shared by all GObjects
#[glib::derived_properties]
impl ObjectImpl for NewMeasurementWindow {
fn constructed(&self) {
println!(
"Value of Spin Button: {}",
self.temperature_spinbutton.value()
);
// Call "constructed" on parent
self.parent_constructed();
}
}
// Trait shared by all widgets
impl WidgetImpl for NewMeasurementWindow {}
// Trait shared by all windows
impl WindowImpl for NewMeasurementWindow {}
In the constructed
method I’m accessing the value of the SpinButton. However I can’t figure out how to access the value outside the implementation module. For example the save_measurement
method
mod imp;
use glib::Object;
use gtk::{gio, glib, prelude::GtkWindowExt};
use crate::main_window::MainWindow;
glib::wrapper! {
pub struct NewMeasurementWindow(ObjectSubclass<imp::NewMeasurementWindow>)
@extends gtk::Window, gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
impl NewMeasurementWindow {
pub fn new(window: &MainWindow) -> Self {
// Create new window
Object::builder().property("transient-for", window).build()
}
pub fn save_measurement(&self) {
// Right now it only prints, needs to be modified to save to database instead
let temperature = self.temperature_spinbutton.value();
println!("Temperature: {}", temperature);
self.close();
}
}
gives me the following error:
error[E0609]: no field `temperature_spinbutton` on type `&new_measurement_window::NewMeasurementWindow`
--> src/new_measurement_window/mod.rs:23:32
|
23 | let temperature = self.temperature_spinbutton.value();
| ^^^^^^^^^^^^^^^^^^^^^^ unknown field
|
= note: available fields are: `inner`, `phantom`
I would greatly appreciate any help!