Global QMetaObject
-
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;
}
@ -
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 = Flowbut it never finds the enum