How to get row in ColumnView for right click

I’m using Rust so appologies for the Rust rather than C++ code here :slight_smile:

Using the following I show a popup when right lick on ColumnView

let gesture = gtk::GestureClick::new();
            gesture.set_button(3);
            gesture.connect_released(clone!(@weak self as view => move | gesture, _n, x, y| {
                gesture.set_state(gtk::EventSequenceState::Claimed);
                if let Some(popover) = view.popover.borrow().as_ref() {
                        popover.set_pointing_to(Some(&Rectangle::new(x as i32, y as i32, 1, 1)));
                        popover.popup();
                };
            }));
            self.airport_list.add_controller(gesture);

I am showing a popup on right click. No problem there, but I cannot find a way to get the row the mouse is over when clicked.

By comparison, for the now deprecated TreeView, the gesture would also select the row when right clicked

Hi,

In the factory’s bind, just make sure to give the row widget a reference to the model’s row item. You may do that with GObject.Object.set_data, or by creating your own row widget subclass with a custom “model item” property.

Then, in the gesture’s callback, you can get the row widget under the mouse with Gtk.Widget.pick, and then the model item with GObject.Object.get_data (or by getting the property if you used a subclass).

Another way is to add gestures to each row widget instead of the columnview.

Then, in bind, connect the callback and pass the model item as user_data.
And disconnect the callback in unbind.

Thanks @gwillems. I tried adding gestures to the row widget, but it appears Gtk.ColumnViewRow is not a widget. I guess the other idea of using Gtk.Widget.pick would have the same issue, but I suppose I could attach required data to each column’s widget instead, but that seems very clumsy.

I’ll try something along those lines.

Sort of works but difficult. Gtk.Widget.pick returns either a Label, a Cell or a Row depending on exactly where the click occurs. I can then get the widget children to get a Label widget, which is where I did the set_data. I then need to get that data and put it into some “global” variable to get in the action. Added to this all the set_data()/data() calls are “unsafe” in Rust terms and I have managed to raise a number of memory access errors.

I think I’ll just change to use Gtk.ColumnView:single-click-activate which avoids the original issue altogether.

1 Like