Solved List all the enum object ?
-
I want to list all the enum object:
for(enum &i : QStyle::StandardPixmap){ //for(int i=0; i<20; i++){ ui->listWidget->addItem(new QListWidgetItem(QIcon(style()->standardPixmap(i)), "QStyle::SP_ArrowBack")); }
-
hi
Use F2 to look at its definition.enum StandardPixmap {
SP_TitleBarMenuButton,
...Its a plain enum and hence it cannot provide the name for you only the value.
So you can grap list and make a map with enum, Text or
simply make a switch case.This is as old an issue as C++ itself.
Tons on the net about it.
https://stackoverflow.com/questions/5093460/how-to-convert-an-enum-type-variable-to-a-string -
@mrjj
His code is not actually attempting to get the names of theenum
values. It needs to know the upper limit of theQStyle::StandardPixmap
values, which seems to beQStyle::SP_LineEditClearButton
at present.... If that's what he means by his question. -
@JNBarchan
Ah, good catch. I was fooled by the "QStyle::SP_ArrowBack" text.
F2 is still his friend :) -
@mrjj
If he's waiting for C++ to tell him the upper "bound" of theenum
, I think he's out of luck, unless you know better. -
@mrjj said in List all the enum object ?:
@JNBarchan
Ah, good catch. I was fooled by the "QStyle::SP_ArrowBack" text.Actually, looking at it, I suspect upon reconsideration that actually your interpretation was correct, and it's the second, "name-of-enum-value" argument to
QListWidgetItem()
he's interested in. I was confused by the commenting out of thefor
loop. Who knows here... ! -
@JNBarchan
Old type enums cannot do anything of that sort and even the new c++ 11 one , only gave it
type safety / type specifier and still no way to get a string version of it.
There might be a template way to make be accepted in for (item : container) construct but
good old for loop seems most appropriate. -
-
Ah, thanks @kshegunov , its flagged with
Q_ENUM(StandardPixmap)
so one can use Qt meta system. -
Yes, sorry for being so cryptic, here you go:
QMetaEnum standardPixmap = QMetaEnum::fromType<QStyle::StandardPixmap>(); for (qint32 i = 0, count = standardPixmap.keyCount(); i < count; i++) qDebug() << standardPixmap.key(i) << endl;
-
It's difficult for C++ to do this. Enum is treated as a value-item, for example, int when the source code is compiled.
Yee, kshegunov's answer with Qt meta system is good, a string map will be pre-compiled in moc step. -
@goldenhawking said in List all the enum object ?:
meta
-Qt's meta compiler makes c++ a "Dynamic" like language , it's very important feature.
-
QMetaEnum metaEnum = QMetaEnum::fromType<QStyle::StandardPixmap>(); for (qint32 i = 0, count = metaEnum.keyCount(); i < count; i++) { QStyle::StandardPixmap SP = (QStyle::StandardPixmap)metaEnum.keyToValue(metaEnum.key(i)); QListWidgetItem *LWI = new QListWidgetItem(QIcon(style()->standardPixmap(SP)), metaEnum.key(i)); LWI->setSizeHint(QSize(100,100)); ui->listWidget->addItem(LWI); }