How to have Gtk.Builder load up AdwAboutWindow easily?

I’ve recently rewrite some Gtk3 code with Gtk4. To enhance theming, I’ve added in AdwAboutWindow (from libadwaita) in my application. Things run fine there. But some of my old unit test gets in the way.

I have a foobar.ui XML with a few other UI widgets and this AdwAboutWindow class definition:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <object class="AdwAboutWindow" id="about_dialog">
    <property name="visible">False</property>
    <property name="application_name">Foobar</property>
    <property name="developer_name">Foobar developers</property>
    <property name="website">https://foobar.org/</property>
    <property name="issue_url">https://github.com/foobar/foobar/issues</property>
    <property name="copyright">© 2024 Foobar Developers</property>
  </object>
</interface>

In the real application, the foobar.ui file was loaded within a method of the “activate” signal within a Adw.Application. Things are fine there.

But in unit test:

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.ui_file = os.path.join(os.environ["PREFERENCES_UI_DIR"],
                                    "foobar.ui")

    def tearDown(self):
        pass

    def test_ui_file_is_valid_gtk_builder(self):
        from gi.repository import Gtk
        b = Gtk.Builder()

        try:
            b.add_from_file(self.ui_file)

        except Exception as e:
            raise AssertionError(e)

I got:

Invalid object type ‘AdwAboutWindow’

What is the simplest way to rewrite this test? Is there a way I can manually load AdwAboutWindow into the GtkBuilder without a full Adw.Applicaiton implementation?

I found the solution.

Turns out I can manually run Adw.init() to load the libawaita’s widget for the builder:

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.ui_file = os.path.join(os.environ["PREFERENCES_UI_DIR"],
                                    "foobar.ui")

    def tearDown(self):
        pass

    def test_ui_file_is_valid_gtk_builder(self):
        from gi.repository import Gtk, Adw
        Adw.init()
        b = Gtk.Builder()

        try:
            b.add_from_file(self.ui_file)

        except Exception as e:
            raise AssertionError(e)

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