How to show popover at a point?

Hii.
I have a drawing area with Cairo drawn content. On a certain event, I would like to pop a popover at a point within the area. This point varies depending on situation, thus not fixed.

This is what I tried (simple example with button and label)

import gi

gi.require_versions({"Adw": "1"})
from gi.repository import Adw, Gdk, Gtk


def on_activate(application):
    win = Adw.ApplicationWindow(application=application)
    pop = Gtk.Popover()
    pop.set_child(Gtk.Label(label="Popover"))
    box = Gtk.Box(
        orientation=Gtk.Orientation.VERTICAL,
        spacing=10,
        margin_top=10,
        margin_end=10,
        margin_bottom=10,
        margin_start=10,
    )
    label = Gtk.Label(label="Click the button to show popover.")
    button = Gtk.Button(label="Show")
    button.connect("clicked", lambda _: pop.popup())
    pop.set_parent(box)
    pop.set_pointing_to(Gdk.Rectangle(100, 100, 10, 10))
    box.append(label)
    box.append(button)
    win.set_content(box)
    win.set_default_size(200, 200)
    win.present()


app = Adw.Application(application_id="com.example.app")
app.connect("activate", on_activate)
app.run([])

But this only produces the following result no matter the values provided to the rectangle.
image
I also get this warning.

DeprecationWarning: Passing arguments to gi.types.Boxed.__init__() is deprecated. All arguments passed will be ignored.
  pop.set_pointing_to(Gdk.Rectangle(100, 100, 10, 10))

Any idea how I can accomplish this?

pop.set_pointing_to(Gdk.Rectangle(100, 100, 10, 10))

You must use keyword arguments in the Rectangle constructor, e.g.

Gdk.Rectangle(width=100, height=100, x=10, y=10)
1 Like

I tried this and it didn’t work.
image
And the warning is still there.

(I actually want to have x and y set to 100 and the dimensions set to 10, I tried that and it still doesn’t work)

Oh wait this works!

...
r = Gdk.Rectangle()
r.x = 100
r.y = 100
r.width = 10
r.height = 10
pop.set_pointing_to(r)
...

If passing arguments that way is deprecated, is this the only way? Feels a little lengthy :thinking:

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