determining widget with current focus?

30 views
Skip to first unread message

jkn

unread,
Aug 20, 2025, 5:50:56 AMAug 20
to leo-editor
Hi all
    is there an easy way of determining where the focus is currently in Leo (body, tree, log...)?

I find the use of individual 'focus-to...' commands, and any shortcuts, to be a bit awkward. I would like to write a 'focus-cycle' type command, with an action a bit like the standard Alt-Tab, but within Leo. However looking at the code it seems that the current focus is not held in a simple way. I thought I'd ask here before delving deeper...

Thanks, J^n

Thomas Passin

unread,
Aug 20, 2025, 9:23:16 AMAug 20
to leo-editor
Check out QApplication.focusWidget(). That could be a starting point. I doubt that Leo itself knows in most cases.

Edward K. Ream

unread,
Aug 20, 2025, 10:51:29 AMAug 20
to leo-e...@googlegroups.com
On Wed, Aug 20, 2025 at 4:50 AM jkn <jkn...@nicorp.co.uk> wrote:
Hi all
    is there an easy way of determining where the focus is currently in Leo (body, tree, log...)?

c.get_focus() or g.app.gui.get_focus() return a widget w. You can then look at w.objectName().

Edward

Edward K. Ream

unread,
Aug 20, 2025, 10:54:00 AMAug 20
to leo-e...@googlegroups.com
On Wed, Aug 20, 2025 at 8:23 AM Thomas Passin <tbp1...@gmail.com> wrote:
Check out QApplication.focusWidget(). That could be a starting point. I doubt that Leo itself knows in most cases.

Leo must be aware of the focus widget in all situations. Binding wouldn't work otherwise.

Edward

Thomas Passin

unread,
Aug 20, 2025, 11:19:47 AMAug 20
to leo-editor
I thought that most of that was handled by the Qt machinery rather than by specific Leo code.  For example, when a user clicks in a text edit box, Qt puts the focus there and directs the input to the box.  Leo gets that input by means of its registered callbacks so it can handle those input events. That's all I meant. In many cases Leo can handle the registered events without learning about the focus because if the focus weren't on the widget, those handlers wouldn't be getting called.

I'm glad to know about those get_focus() methods. I don't think I've used them or even known about them.

jkn

unread,
Aug 20, 2025, 12:22:41 PMAug 20
to leo-editor

Thanks Edward, that sounds very helpful.

    J

Edward K. Ream

unread,
Aug 20, 2025, 12:50:50 PMAug 20
to leo-e...@googlegroups.com
On Wed, Aug 20, 2025 at 11:22 AM jkn <jkn...@nicorp.co.uk> wrote:

Thanks Edward, that sounds very helpful.

You're very welcome. Yours was an excellent question.

Edward

jkn

unread,
Aug 20, 2025, 1:00:59 PMAug 20
to leo-editor
Heh...

AttributeError: 'LeoQtBody' object has no attribute 'objectName'

jkn

unread,
Aug 20, 2025, 1:54:07 PMAug 20
to leo-editor

I see that 

    c.get_focus()

and

    g.app.gui.get_focus()


actually return different things...

Thomas Passin

unread,
Aug 20, 2025, 6:00:32 PMAug 20
to leo-editor
On Wednesday, August 20, 2025 at 1:00:59 PM UTC-4 jkn wrote:
Heh...

AttributeError: 'LeoQtBody' object has no attribute 'objectName'


w.widget.objectName() 

Edward K. Ream

unread,
Aug 20, 2025, 6:02:31 PMAug 20
to leo-e...@googlegroups.com
Thanks, Thomas.  Apparently w is a Wrapper object.

Edward

jkn

unread,
Aug 20, 2025, 6:29:08 PMAug 20
to leo-editor
slightly teasing here:

AttributeError: 'LeoQTreeWidget' object has no attribute 'widget'

Thomas Passin

unread,
Aug 20, 2025, 8:17:51 PMAug 20
to leo-editor
Then try for objectName() directly. The name  "LeoQTreeWidget" suggests that it's a actual widget, not a wrapped one.

BTW, the way I learned that LeoQtBody had a widget attribute was to run dir() on it. "widget" showed up at the end of the list.

jkn

unread,
Aug 21, 2025, 2:50:47 AMAug 21
to leo-editor
Sure - I was just pointing out that the calls seem to return different things for different 'focus parts' of the system.

Edward K. Ream

unread,
Aug 21, 2025, 5:42:30 AMAug 21
to leo-e...@googlegroups.com
On Thu, Aug 21, 2025 at 1:50 AM jkn <jkn...@nicorp.co.uk> wrote:
Sure - I was just pointing out that the calls seem to return different things for different 'focus parts' of the system.

Thanks to you and Thomas for your comments.

My responses were hasty and not too helpful. The only sure reference is the actual code :-)

The signature in question is:  class LeoQTreeWidget(QtWidgets.QTreeWidget):

So yes, a LeoQTreeWidget is a QTreeWidget. And (indirectly!) a QTreeWidget is a QWidget.

I'm not going to change the code. Devs should decide whether g.app.gui.get_focus or c.get_focus is appropriate.

Edward

jkn

unread,
Aug 21, 2025, 5:59:25 AMAug 21
to leo-editor
Yes, it looks like some simple heuristic may be needed. And I am conscious that some plugins, that I may or may not be aware of, may add panes and complications.

Still, it's my own itch that I am scratching, for now anyway. Let's see what I come up with...

Thanks, J
Reply all
Reply to author
Forward
0 new messages