[Qt-interest] Getting enum names as String for Qt:: identifyer

J-P Nurmi jpnurmi at gmail.com
Tue Jun 15 14:32:10 CEST 2010


On Tue, Jun 15, 2010 at 2:31 PM, Justus Best <just18 at gmx.de> wrote:
> Hi,
>
> Is there a possibility to get the names of enums declared in <QtCore/Qt> ?
>
> I found out that I can get enum names for a MetaObject, but I guess
> there is no MetaObject generated with enums in the Qt Namespace.
> Would have liked it to display meaningful descriptions for QBrushes
> without using a long switch case to return a string representation.
>
> E.g. nameOfBrush(QBrush b) returning "SolidPattern" if b's style is
> Qt::SolidPattern.
>

Hi Justus,

It gets a bit hackish... but in theory it is possible via
(undocumented and protected) QObject::staticQtMetaObject.

// in QObject context (ie. in a QObject subclass):
int index = QObject::staticQtMetaObject.indexOfEnumerator("BrushStyle");
QMetaEnum metaEnum = QObject::staticQtMetaObject.enumerator(index);
const char* key = metaEnum.valueToKey(Qt::SolidPattern);
qDebug() << key; // "SolidPattern"

WARNING: This feature is not part of the public and documented Qt API... ;-)

--
J-P Nurmi



More information about the Qt-interest-old mailing list