Simple question as in the title - if I call setObjectName()
on an object, does it have to be unique, or is it just recommended because of convention? I've subclassed QLabel
, and want to automatically give the objects created a name; if this is a bad idea, I'll find some way of setting a random unique name. (I'm actually using PyQt, but this shouldn't influence the answer!)
Update
In response to the 2 answers so far (which I should have predicted!), I want to do this so that I can ignore all instances of my subclass. Calling findChildren(QLabel)
cascades so that all subclasses of QLabel
are also found - if I use the same object name for all my subclassed objects, then I can just regex in the 2nd argument of findChildren()
so that I ignore them
If you look at the documentation for QObject, you can see that it states: -
This property holds the name of this object. You can find an object by name (and type) using findChild(). You can find a set of objects with findChildren().
Looking at findChildren(), it states: -
Returns all children of this object with the given name that can be cast to type T...
So, clearly it can be seen that multiple objects can have the same name.
No, but without unique names you wouldn't be able to find objects by name (without additional checks).
Setting an object name is not strictly needed, it defaults to an empty string. However, if you need to find objects by their name, you surely want it to be unique!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With