Template not working properly

I’m probably trying to do something I shouldn’t but I’m trying anyway!

Essentially, I want to make a template for an AdwExpanderRow and it’s ‘on-the-way’ but the gtype_name doesn’t seem to be working right. If I debug my Python module I get…

RuntimeError: could not create new GType: ContactRow (subclass of GtkWidget)

My code it kind of working, I get a row in my GtkListBox, but not the row in my template… presumably because it ‘could not create new GType…’

Initially, I was using an sql database and loading it into a GListStore then binding the GListStore and a definition that creates an AdwExpanderRow into a model for the GtkListBox. Whenever I wanted to re/load the GtkListBox I simply used…

for row in record:
        liststore.append(ContactDataObject(row))

It worked great. I setup a callback so that when you click on a row it prints the row’s name (in my case the sql rowid) but I have a huge amount of code in my .py file that creates the AdwExpanderRow. I don’t like looking at it!

So, I decided to make a template of a GtkWidget whose child is an AdwExpanderRow and try binding that with the GtkListStore.

If I run the app I get a blank row in my GtkListBox but when I click on it the callback prints “GtkListBoxRow”. It does, however, throw up…

(main.py:3204): Gtk-CRITICAL **: 15:03:40.108: Error building template class 'ContactRow' for an instance of type 'ContactRow': .:0:0 'version' attribute has malformed value '1'

When I’ve created the AdwExpanderRows in my .py file without filling it with data from the sql database it would read AdwExpanderRow so in the case of my template I would expect the same, not a Gtk row.

To try to keep it brief, this is what I have…

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk" version="4.0"/>
  <requires lib="libadwaita" version="1"/>
   <template class="ContactRow" parent="GtkWidget">
   <child>
    <object class="AdwExpanderRow" id="contact_expander_row">
    <child> Lots of lines that provide the AdwExpanderRow content.....

@Gtk.Template(filename='expander.ui')
class ContactRow(Gtk.Widget):
    __gtype_name__ = "ContactRow"
    
    contact_expander_row = Gtk.Template.Child()

    def __init__(self, row):
        super().__init__()

I’ve got the expander.ui file in the same directory as my .py modules and window.ui and there’s no errors related to not finding the .ui file.

Any ideas would be appreciated! Thank you

Try

<requires lib="libadwaita" version="1.0"/>

The last error message says that the attribute version with the value 1 is invalid, which only appears in your .ui file for the libadwaita <requires> thing. According to the GtkBuilder documentation, the format is major.minor, so like version="1.0". libadwaita’s example .ui file also does this, so try that.

Also I don’t think that you can just inherit GtkWidget and have a child widget without further code, try inheriting from AdwBin instead.

1 Like

Thank you!

That got rid of the CRITICAL error, version=1.2 did it.

I’ve managed to get it to display the AdWExpanderRow now complate with it’s contents and the callback is printing the class name (“ContactRow”). I got rid of the GtkWidget part so it’s now…

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk" version="4.0"/>
  <requires lib="libadwaita" version="1.2"/>
   <template class="ContactRow" parent="AdwExpanderRow">

and…

@Gtk.Template(filename='expander.ui')
class ContactRow(Adw.ExpanderRow):
    __gtype_name__ = "ContactRow"

    #contact_expander_row = Gtk.Template.Child()

    def __init__(self, row):
        super().__init__()
        
        #print(row[1])
        #self.contact_expander_row.set_name("dave")
        #self.contact_expander_row.set_title(row[1])

but it still ‘could not create new GType: ContactRow’ so I can’t get access the childs

Actually, it’s working. As long as I can forget about that debug error(!) and work out how to handle the database. It’s saved 120 lines.

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