views:

331

answers:

6

I am wondering why Qt uses Q before every class name rather than putting everything in a namespace. Is there any particular reason, such as making the names easy to search for, or is it just about brand names?

+3  A: 

It may be a portability issue. Namespaces weren't / aren't supported by every compiler, so the naming convention helps to cut down on naming clashes.

Glen
by default the code generated from Qt Designer uses ui:: namespace ...I suspect that may not be the problem
yesraaj
Yep, and Qt _does_ have a `Qt` namespace. It just doesn't contain the classes, only some enums.
+4  A: 

I believe it is historical. Namespaces were introduced into C++ around 1995. Qt development started in 1991 so namespaces could not be used, obviously.

Rafał Dowgird
+2  A: 

Qt is very conservative on the C++ language features it uses. No namespaces, exceptions or RTTI. See also this article detailing why templates are not used in signal/slot handling.

Paul Dixon
Although I agree that Qt is conservative, your link to no templates is specific to the signal/slot mechanism. Qt definitely uses templates in a lot of their code.The exceptions and RTTI stuff is due to being used on embedded devices, I would wager. Many embedded apps avoid those for the overhead they add.
Caleb Huitt - cjhuitt
That's a good point, and correct. Will amend answer!
Paul Dixon
A: 

Seeing as there's not a single C++ compiler left that doesn't implement namespaces, nowadays there's only one reason: Branding :)

no current compiler maybe. but there's plenty of people still using older compilers who won't / can't upgrade to the latest version. I'm not saying it's a good situation, but sometimes you're just stuck with using a crappy old compiler, for any number of technical / non-technical reasons.
Glen
Even if everyone used new compilers that do support namespaces correctly (and I disagree with your statement BTW - there are still current compilers that do not support all the nuances of namespaces correctly), there's still a *lot* of Qt code out that that uses the current naming convention. Do you expect everyone to change all their Qt code to a new naming convention just to use namespaces?
Thomi
@Glen, Thomi: Qt does use namespaces nowadays (since 4.0), so any compiler left without at least basic support is­ no longer supported by Qt anyway (cannot be supported).
+2  A: 

The documentation for Qt refers to namespaces, although I didn't check the code to see if they are truly c++ namespaces or a hack with public declarations inside a class. I would guess that the rest is trying to avoid causing everybody to need to rename everything, although they could provide a migration path if they wanted to, like so:

namespace Qt
{
class Object { ... };
}

#ifndef NO_OLD_DECLS
typedef Qt::Object QObject;
#endif
Caleb Huitt - cjhuitt
A: 

Qt uses a Q prefix as part of their coding style. It usually serves the purpose of making it easier to read the code and spot what is what.

An identifier that:

  • is prefixed with "Q" and suffixed with "Private" is a private class used for implementation details and is not part of the API (e.g. QPainterPrivate)
  • is prefixed with "Q" and not suffixed with "Private" is a public class (e.g. QWidget)
  • is prefixed with "q" (lowercase) is a public global function (e.g. qRgb)

Adopting a coding style and using it uniformly makes it much easier for other people to understand code they didn't write.

Ref.: Qt Coding Style

Fred