Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Qt ObjectName() have to be unique?

Tags:

qt

pyqt

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

like image 907
ChrisW Avatar asked Oct 14 '14 15:10

ChrisW


3 Answers

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.

like image 154
TheDarkKnight Avatar answered Nov 12 '22 18:11

TheDarkKnight


No, but without unique names you wouldn't be able to find objects by name (without additional checks).

like image 41
Oleg Shparber Avatar answered Nov 12 '22 18:11

Oleg Shparber


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!

like image 39
Luigi Belli Avatar answered Nov 12 '22 17:11

Luigi Belli