How to detect the type of widget?

There are several ways to get the name of the widget:

  • using __class__:

print(self.lbl.__class__.__name__)
  • using QMetaObject:

print(self.lbl.metaObject().className())

These previous methods return a string with the name of the class, but if you want to verify if an object belongs to a class you can use isinstance():

is_label = isinstance(self.lbl, QLabel)

Another option is to use type() but it is not recommended, if you want to get more information about isinstance() and type() read the following: What are the differences between type() and isinstance()?


You can just use the standard Python means of checking an object type:

print(type(self.lbl))
print(isinstance(self.lbl, QLabel)