Multiple Window - Action received in wrong window

Hi,
I am developing my first GTK4/Libadwaita Application with python.

It is intended to have the main application window being opened multiple times with different data loaded.
Now i am facing the issues that some actions are received in the wrong window.
This only happens though, with actions i “installed” to a widget using the “install_action()” method.

self.install_action('panel.search', None, self._on_actions)
self.install_action('panel.add', None, self._on_actions)
self.install_action('panel.edit', None, self._on_actions)

I have those 3 actions installed to a subclassed widget for example.
They are then used with the “action-name” property in some buttons in a UI Template.

Actions i have added to the main Adw.ApplicationWindow using the following method

action = Gio.SimpleAction.new('save', None)
action.connect("activate", self.on_save)
self.add_action(action)

don’t have this issue. They are always received in the correct (active) window.

Sadly my code is on a private gitlab instance atm, so i can’t share the whole application.
I hope someone with more experience can still point me in the right direction tho :slight_smile:

Hi,

install_action is a classmethod, i.e. shared with all classes instances.
So all instances will call the very same self._on_actions at the end, whatever self was when the action was installed.

You shall instead create an action group panel, add it to the widget, and add the actions to it:

agroup = Gio.SimpleActionGroup()
self.insert_action_group('panel', agroup)

for action_name in ('search', 'add', 'edit'):
    action = Gio.SimpleAction.new(action_name, None)
    action.connect("activate", self._on_actions)
    agroup.add_action(action)

Thank you very much for the explanation and the snippet with the correct method.

I actually tried setting up everything this way but missed the action group part as this was not needed for AdwApplicationWindow.

I found the install_action method in some other c gtk code which i ported to python (color button with a color chooser but i configurable palette) and it worked great until i noticed the issue with multiple app windows .

Ah yes, ApplicationWindow automatically creates a win group, so no need to manually add one.

When using GObject in C, you can specify a class_init function to call classmethods like install_action. That allows to setup once at the program init all the common class stuff that’s independent of the future object instances.

This mechanism does not exist in Python AFAIK. The only way I know to use such classmethods is to first declare the class then call the classmethod from outside:

class MyWidget(Gtk.Widget):
    # class implementation here
    ...

MyWidget.install_action('panel.search', None, on_actions)

with on_action independent of the instance (i.e. no “self”), so either a free function or a staticmethod/classmethod of MyWidget.

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