Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do the names of Qt's objects have to be unique?

Tags:

object

unique

qt

Does the QObject::objectName property have to be unique for the whole application? For example, let's say I have button somewhere called "new", then somewhere else I'm going to create a QShortcut also called "new". Is it going to cause a problem to Qt?

I know about properly naming objects (something called "new" is not a good name) but I just want to know whether I need to be extra careful or not.

like image 919
laurent Avatar asked Aug 05 '11 14:08

laurent


2 Answers

Object names are not required to be unique. However, there are at least two things I can think of to consider when naming your objects:

  • QObject::findChild() - A function where you can search for QObjects by name.
  • Style sheets. If you ever specify a style sheet for a widget by name, it will apply to objects in the hierarchy underneath the widget with that style that have that name.
like image 192
Chris Avatar answered Nov 13 '22 04:11

Chris


Other things to consider:

  • Objects to not require names. If you're not using the names in any meaningful way, you don't have to set them. I normally don't set them for one-off objects like QTimers and such.
  • If you're using designer to make a .ui file (doesn't sound like you are, but just in case), uic tends to spit out warnings for duplicate names. So if you don't want to see those warnings, keep the names in the .ui file unique (designer tends to enforce this by appending _1, _2, etc to duplicate names).
like image 4
Scott Minster Avatar answered Nov 13 '22 03:11

Scott Minster