TemplateChild not found

Whenever I run the app, I get an error saying,

Gtk-CRITICAL **: 11:16:16.528: Error building template class 'SmartShredsWindow' for an instance of type 'SmartShredsWindow': .:0:0 Object with ID  not found
thread 'main' panicked at /Users/joelkomieter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/gtk4-0.8.2/src/subclass/widget.rs:1254:17:
Failed to retrieve template child. Please check that all fields of type `GtkEntry` have been bound and have a #[template_child] attribute.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Although I’ve made sure to check the Ids are corresponsing to their names in the Window imp struct.

<child>
  <object class="GtkEntry" id="entry">
    <property name="margin-end">10</property>
    <property name="placeholder-text" translatable="yes">Search files...</property>
  </object>
</child>
<child>
  <object class="GtkButton" id="dir_button">
    <property name="label" translatable="yes">Choose directory to scan</property>
  </object>
</child>

Also the app can’t find the Object Id for the Window class:

Error building template class 'SmartShredsWindow' for an instance of type 'SmartShredsWindow': .:0:0 Object with ID  not found

Can you post the full xml file of the template? The excerpt you posted is correct, but the problem may lie elsewhere.

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <template class="SmartShredsWindow" parent="AdwApplicationWindow">
    <property name="title" translatable="yes">SmartShreds</property>
    <property name="width-request">900</property>
    <property name="height-request">800</property>
    <child>
        <object class="AdwOverlaySplitView" id="split_view">
            <property name="sidebar">
                <object class="GtkListBox">
                    <property name="selection-mode">single</property>
                    <child>
                        <object class="GtkListBoxRow">
                            <property name="activatable">false</property>
                            <property name="selectable">false</property>
                            <property name="margin-top">20</property>
                            <property name="margin-bottom">50</property>
                            <child>
                                <object class="GtkLabel">
                                    <property name="label" translatable="yes">SmartShreds</property>
                                    <property name="name">main-title</property>
                                </object>
                            </child>
                        </object>
                    </child>
                    <child>
                        <object class="GtkListBoxRow">
                            <property name="selectable">true</property>
                            <property name="activatable">true</property>
                            <child>
                                <object class="GtkBox">
                                    <property name="orientation">horizontal</property>
                                    <property name="name">box-row</property>
                                    <child>
                                        <object class="GtkImage">
                                            <property name="icon-name">document-open-symbolic</property>
                                        </object>
                                    </child>
                                    <child>
                                        <object class="GtkLabel">
                                            <property name="label" translatable="yes">Find duplicates</property>
                                            <property name="name">label-row</property>
                                        </object>
                                    </child>
                                </object>
                            </child>
                        </object>
                    </child>
                    <child>
                        <object class="GtkListBoxRow">
                            <property name="selectable">true</property>
                            <property name="activatable">true</property>
                            <child>
                                <object class="GtkBox">
                                    <property name="orientation">horizontal</property>
                                    <property name="name">box-row</property>
                                    <child>
                                        <object class="GtkImage">
                                            <property name="icon-name">document-open-symbolic</property>
                                        </object>
                                    </child>
                                    <child>
                                        <object class="GtkLabel">
                                            <property name="label" translatable="yes">Smart organize</property>
                                            <property name="name">label-row</property>
                                        </object>
                                    </child>
                                </object>
                            </child>
                        </object>
                    </child>
                    <child>
                        <object class="GtkListBoxRow">
                            <property name="selectable">true</property>
                            <property name="activatable">true</property>
                            <child>
                                <object class="GtkBox">
                                    <property name="orientation">horizontal</property>
                                    <property name="name">box-row</property>
                                    <child>
                                        <object class="GtkImage">
                                            <property name="icon-name">document-open-symbolic</property>
                                        </object>
                                    </child>
                                    <child>
                                        <object class="GtkLabel">
                                            <property name="label" translatable="yes">Shred files</property>
                                            <property name="name">label-row</property>
                                        </object>
                                    </child>
                                </object>
                            </child>
                        </object>
                    </child>
                </object>
            </property>
            <property name="content">
                <object class="AdwNavigationView" id="pages">
                    <!-- Start of Find Duplicate page -->
                    <child>
                        <object class="AdwNavigationPage">
                        <property name="title" translatable="yes">Find duplicates</property>
                        <property name="child">
                            <object class="AdwToolbarView">
                            <child type="top">
                                <object class="AdwHeaderBar"/>
                            </child>
                            <property name="content">
                                <object class="GtkBox">
                                    <property name="orientation">vertical</property>
                                    <style>
                                        <class name="spacer"/>
                                    </style>
                                    <child>
                                    <!-- Header -->
                                        <object class="GtkCenterBox">
                                            <property name="orientation">horizontal</property>
                                            <property name="name">header-row</property>
                                            <child type="start">
                                                <object class="GtkLabel">
                                                    <property name="label" translatable="yes">Duplicate file detection</property>
                                                    <property name="name">header-title</property>
                                                </object>
                                            </child>
                                            <child type="end">
                                                <object class="GtkBox">
                                                    <property name="orientation">horizontal</property>
                                                    <child>
                                                        <object class="GtkEntry" id="entry">
                                                            <property name="margin-end">10</property>
                                                            <property name="placeholder-text" translatable="yes">Search files...</property>
                                                        </object>
                                                    </child>
                                                    <child>
                                                        <object class="GtkButton" id="dir_button">
                                                            <property name="label" translatable="yes">Choose directory to scan</property>
                                                        </object>
                                                    </child>
                                                </object>
                                            </child>
                                        </object>
                                    <!-- Header -->
                                    </child>
                                </object>
                            </property>
                            </object>
                        </property>
                        </object>
                    </child>
                    <!-- End of Find Duplicate page -->
                    <child>
                        <object class="AdwNavigationPage">
                        <property name="title" translatable="yes">Page 2</property>
                        <property name="tag">page-2</property>
                        <property name="child">
                            <object class="AdwToolbarView">
                            <child type="top">
                                <object class="AdwHeaderBar"/>
                            </child>
                            <property name="content">
                                <!-- ... -->
                            </property>
                            </object>
                        </property>
                        </object>
                    </child>
                </object>
            </property>
        </object>
    </child>
  </template>
</interface>

use gtk::{self, glib::{self, subclass::InitializingObject}, Button, CompositeTemplate, Entry};
use adw::subclass::prelude::*;

#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/gtk_rs/SmartShreds/window.ui")]
pub struct SmartShredsWindow {
    #[template_child]
    pub dir_button: TemplateChild<Button>,
    #[template_child]
    pub entry: TemplateChild<Entry>,
}

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

    fn class_init(klass: &mut Self::Class) {
        klass.bind_template();
        // Action to scan a chosen directory.
        klass.install_action_async("start-scan", None, |window, _, _| async move {
            window.select_folder_to_scan().await;
        });
    }

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

impl ObjectImpl for SmartShredsWindow {
    fn constructed(&self) {
        self.parent_constructed();
    }
}

impl WidgetImpl for SmartShredsWindow {}

impl ApplicationWindowImpl for SmartShredsWindow {}

impl AdwApplicationWindowImpl for SmartShredsWindow {}

impl WindowImpl for SmartShredsWindow {}


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.