5

In Our PyQt5 Programme, we use Qline Edits, QcheckBox, QListwidget several times.

How to know:

  1. What are the widgets used in Our Programme with Widget names ?
  2. How to get/returns the name of the widget(user-assigned name) which currently has the focus
2
  • QApplication.focusWidget() will return the widget that currently has the keyboard input focus (or None if no widget has focus). In python, objects have no knowledge about the names of the variables that are referring to them, but for QObjects you could use the objectName property to distinguish between your widgets. Commented May 8, 2020 at 9:22
  • @HEike - Cant get it Commented May 8, 2020 at 13:12

1 Answer 1

6

As @Heike suggested, you can get a reference to the widget that has focus with QApplication.focusWidget

Depending on how your widgets are created, they might not have an objectName. If you use a GUI like Designer or Creator to drop your widgets on a form, then you will have the object name set. However, if you are creating your form in code, you may not have objectName set at all. In that case, you can just make sure to set the objectName in your code. You can see this question of mine for a discussion of setting objectName but to cut to the chase, you can use objectName as a keyword argument when you declare your widget in code, e.g.:

self.MyWidget = QWidget(objectName = MyWidget) 

and later on if you want to get name of the widget that has focus you would use

widgetname = self.focusWidget().objectName() 

or you could just do something with the reference:

widget = self.focusWidget() 
Sign up to request clarification or add additional context in comments.

3 Comments

Hai @bfris, Its work Perfectly. But at the same time also I need , all widgets names used in a window/frame/layout.
and also get the widget Object name when focus is changing from one widget to another widget
@Bala look in the docs for focusChanged and findChildrenor ask another question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.