[Qt-interest] Multiple Objects all having the Same name
Michael Jackson
imikejackson at gmail.com
Wed Feb 15 15:38:42 CET 2012
On Feb 15, 2012, at 1:33 AM, Bo Thorsen wrote:
> Den 14-02-2012 21:30, Michael Jackson skrev:
>> Is it a "Bad Idea" to have multiple objects all have the same Object
>> Name? I am writing some code and trying to solve a problem where the
>> easiest solution would be to have some of my GUI widgets all have the
>> same Object Name. But I am thinking this is a "really bad" idea.
>
> It is probably a bad idea, because
>
> - I can't see a single good reason to do it
> - You can't search for objects by name (yes, this is something that is actually done for good reasons every now and then)
> - Tools like Squish won't work
> - You can't do this in designer
> - It will confuse other Qt coders, because it's so non-standard
>
> If you don't care about those, then there are no technical reasons against it. The object name isn't used for anything important, and it's not stored somewhere for global access, or something like it. Actually, most QObject instantiations probably have the same object name: The empty string.
>
> The only good reason I can find for this is to do something like this:
>
> findChildren<QWidget*>("The Big Name");
>
> But this can be accomplished by not using a name at all, setting a property on those you want to handle, and only work on those in the loop, or - if it's your own classes - an interface declaration.
>
> If I were your boss, you would have a *very* hard time convincing me that this is a good idea, but I'm not saying that it's impossible :-)
>
> Bo Thorsen,
> Fionia Software.
>
> --
I knew it was a bad idea but at the time I just needed to step away for a bit and give the problem some thought. As probably expected, signals/slots was the solution where I dynamically create the name of an existing slot based on the property and then connect the signal from the widget to the dynamically created slot name. Much cleaner approach. Just proves once again that one should not program when their head isn't clear.
Thanks to all for the clarifications and help
Mike Jackson
More information about the Qt-interest-old
mailing list