user1095108
user1095108

Reputation: 14603

meta-type information for the Qt namespace

If you check the qnamespace.h from Qt source code, you get to see something like this:

#ifndef Q_MOC_RUN
namespace
#else
class Q_CORE_EXPORT
#endif
Qt {

#if defined(Q_MOC_RUN)
    Q_OBJECT
#endif

#if (defined(Q_MOC_RUN) || defined(QT_JAMBI_RUN))
    // NOTE: Generally, do not add Q_ENUMS if a corresponding Q_FLAGS exists.
    Q_ENUMS(ScrollBarPolicy FocusPolicy ContextMenuPolicy)
    Q_ENUMS(ArrowType ToolButtonStyle PenStyle PenCapStyle PenJoinStyle BrushStyle)
    Q_ENUMS(FillRule MaskMode BGMode ClipOperation SizeMode)
    Q_ENUMS(BackgroundMode) // Qt3

My interpretation of this code is, that the moc preprocessor is fooled into generating meta-type information for a fake Qt object. How can I access this "fake" meta-object to get, for example, a QMetaEnum for ArrowType and other enums?

Upvotes: 1

Views: 839

Answers (1)

user1095108
user1095108

Reputation: 14603

The code below does it. The output is:

LeftArrow
#include <QtCore/QTextStream>
#include <QtCore/QMetaEnum>

struct StaticQtMetaObject : public QObject
{
    static inline const QMetaObject& get() {return staticQtMetaObject;}
};

int main(int argc, char *argv[])
{
    const QMetaObject& mo = StaticQtMetaObject::get();
    int index = mo.indexOfEnumerator("ArrowType");
    QMetaEnum me = mo.enumerator(index);
    Qt::ArrowType arrowType = Qt::LeftArrow;
    QTextStream(stdout) << me.valueToKey(arrowType) << endl;
    return 0;
}

Courtesy of http://qt-project.org/forums/viewthread/658

Upvotes: 2

Related Questions