Global QMetaObject



  • Is there a way to get a global QMetaObject from which ich can get the QMetaEnum for Qt::Alignment to get a String representation of my alignment or an Qt::Alignment from a String ?



  • Of course:

    @
    #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("Alignment");
    QMetaEnum me = mo.enumerator(index);
    Qt::Alignment alignment = Qt::AlignLeft;
    QTextStream(stdout) << me.valueToKey(alignment) << endl;
    return 0;
    }
    @



  • Thank you !

    I didn't know that a staticQtMetaObject exists.



  • Am I understanding this right? This only gets enums from Qt name space right?

    How would I do the same thing for QListView::ViewMode??



  • There is a QListView::staticMetaObject



  • Hmm, that is what I thought but my code does not work - am I doing something blonde? :-)

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

    class StaticQtMetaAbstractItemView : public QAbstractItemView
    {
    public:
    static inline const QMetaObject& get() {return staticQtMetaObject;}
    };@
    @int CGuide::getEnumNumber(QString enumString, QString enumName)
    {
    int indexValue(0);
    int index = StaticQtMetaObject::get().indexOfEnumerator(enumName.toStdString().c_str());
    if(index >= 0)//the enum found
    {
    QMetaEnum metaEnum = StaticQtMetaObject::get().enumerator(index);
    indexValue = metaEnum.keyToValue(enumString.toStdString().c_str());
    }
    else
    {
    index = StaticQtMetaAbstractItemView::get().indexOfEnumerator(enumName.toStdString().c_str());
    if(index >= 0)//the enum found
    {
    QMetaEnum metaEnum = StaticQtMetaObject::get().enumerator(index);
    indexValue = metaEnum.keyToValue(enumString.toStdString().c_str());
    }
    }
    return indexValue >= 0 ? indexValue : 0;//always default - get negative numbers if code above fails
    }@



  • The second
    @QMetaEnum metaEnum = StaticQtMetaObject::get().enumerator(index);@

    should be

    @QMetaEnum metaEnum = StaticQtMetaAbstractItemView::get().enumerator(index);@

    I think you dont need the StaticQtMetaAbstractItemView class and you can just use QAbstractItemView::staticMetaObject.
    If you use it, it has to look like this:

    @class StaticQtMetaAbstractItemView : public QAbstractItemView
    {
    public:
    static inline const QMetaObject& get() {return staticMetaObject;}
    };@

    instead of
    @ static inline const QMetaObject& get() {return staticQtMetaObject;}@

    staticQtMetaObject is just a special case for the Qt namespace



  • So I should be able to write

    @int index = QAbstractItemView::staticMetaObject.indexOfEnumerator(enumName.toStdString().c_str());
    QMetaEnum metaEnum = StaticQtMetaAbstractItemView::get().enumerator(index);@

    and no other code??

    Tried this with:

    enumName = QListView::Flow
    and
    enumName = Flow

    but it never finds the enum


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.