QT how to take enum object inside the QList and print accordingly ?
-
wrote on 31 Jul 2018, 14:00 last edited by
I have a header file. inside i will declare the enum class like :
class demo { enum {a,b,c,d}; }
and i have a demo.cpp file and i need to print the enum value. When inside the enum class i have only for string then i will decalre inside the .cpp like
char const * string[]={"a","b","c","d"}
If i have more 1000 object there inside the enum then how will I call and print all value inside demo.cpp file. I viewed many option but I am not satisfied with answer. If some give answer its very useful for me. Thank you in advance
-
I have a header file. inside i will declare the enum class like :
class demo { enum {a,b,c,d}; }
and i have a demo.cpp file and i need to print the enum value. When inside the enum class i have only for string then i will decalre inside the .cpp like
char const * string[]={"a","b","c","d"}
If i have more 1000 object there inside the enum then how will I call and print all value inside demo.cpp file. I viewed many option but I am not satisfied with answer. If some give answer its very useful for me. Thank you in advance
inside a Q_OBJECT, Q_GADGET or Q_NAMESPACE:
enum MyEnum { a, .... zzz9999 }; Q_ENUM(MyEnum)
then use QMetaEnum to get values or values-to-strings, etc...
QMetaEnum e = QMetaEnum::fromType<MyEnum>(); qDebug() << e.valueToKey(MyEnum::zzz9999);
-
inside a Q_OBJECT, Q_GADGET or Q_NAMESPACE:
enum MyEnum { a, .... zzz9999 }; Q_ENUM(MyEnum)
then use QMetaEnum to get values or values-to-strings, etc...
QMetaEnum e = QMetaEnum::fromType<MyEnum>(); qDebug() << e.valueToKey(MyEnum::zzz9999);
wrote on 31 Jul 2018, 14:11 last edited by amarism@raven-worx I am getting error when i select the Q_ENUM() below the enum. and my enum is there inside the "namespace"
-
@raven-worx I am getting error when i select the Q_ENUM() below the enum. and my enum is there inside the "namespace"
@amarism
show some code -
@amarism
show some codewrote on 1 Aug 2018, 06:13 last edited by amarism 8 Jan 2018, 06:16@raven-worx this on my .h file
namespace DC { enum EnumType { FileMetaInformationGroupLength = 0x00020000, FileMetaInformationVersion = 0x00020001, MediaStorageSOPClassUID = 0x00020002, MediaStorageSOPInstanceUID = 0x00020003 .......... 5000 }; Q_ENUM(EnumType); }
and I am reading the file staticlly here in .cpp file
QString readFile::SetTagNames(QStandardItemModel *model) { char *DisplayTags[] = { " FileMetaInformationGroupLength ", "FileMetaInformationVersion", "MediaStorageSOPClassUID" , "MediaStorageSOPInstanceUID", "ImplementationClassUID"}; for (int i = 0; i < total ; i++) { model->setData(model->index(i, 0), DisplayTags[i]); } return QString(); }
reading respective date through
model->setData(model->index(0, 1), pMetaData->GetAttributeValue(DC::FileMetaInformationGroupLength ).GetCharData()); model->setData(model->index(1, 1), pMetaData->GetAttributeValue(DC::FileMetaInformationVersion).GetCharData()); model->setData(model->index(2, 1), pMetaData->GetAttributeValue(DC::MediaStorageSOPClassUID).GetCharData()); model->setData(model->index(3, 1), pMetaData->GetAttributeValue(DC::MediaStorageSOPInstanceUID).GetCharData());
-
@raven-worx this on my .h file
namespace DC { enum EnumType { FileMetaInformationGroupLength = 0x00020000, FileMetaInformationVersion = 0x00020001, MediaStorageSOPClassUID = 0x00020002, MediaStorageSOPInstanceUID = 0x00020003 .......... 5000 }; Q_ENUM(EnumType); }
and I am reading the file staticlly here in .cpp file
QString readFile::SetTagNames(QStandardItemModel *model) { char *DisplayTags[] = { " FileMetaInformationGroupLength ", "FileMetaInformationVersion", "MediaStorageSOPClassUID" , "MediaStorageSOPInstanceUID", "ImplementationClassUID"}; for (int i = 0; i < total ; i++) { model->setData(model->index(i, 0), DisplayTags[i]); } return QString(); }
reading respective date through
model->setData(model->index(0, 1), pMetaData->GetAttributeValue(DC::FileMetaInformationGroupLength ).GetCharData()); model->setData(model->index(1, 1), pMetaData->GetAttributeValue(DC::FileMetaInformationVersion).GetCharData()); model->setData(model->index(2, 1), pMetaData->GetAttributeValue(DC::MediaStorageSOPClassUID).GetCharData()); model->setData(model->index(3, 1), pMetaData->GetAttributeValue(DC::MediaStorageSOPInstanceUID).GetCharData());
@amarism said in QT how to take enum object inside the QList and print accordingly ?:
namespace DC
{
enum EnumType {
FileMetaInformationGroupLength = 0x00020000,
FileMetaInformationVersion = 0x00020001,
MediaStorageSOPClassUID = 0x00020002,
MediaStorageSOPInstanceUID = 0x00020003
.......... 5000
};
Q_ENUM(EnumType);
}as i mentioned you should use
Q_NAMESPACE
!namespace DC { Q_NAMESPACE ... }
-
inside a Q_OBJECT, Q_GADGET or Q_NAMESPACE:
enum MyEnum { a, .... zzz9999 }; Q_ENUM(MyEnum)
then use QMetaEnum to get values or values-to-strings, etc...
QMetaEnum e = QMetaEnum::fromType<MyEnum>(); qDebug() << e.valueToKey(MyEnum::zzz9999);
wrote on 1 Aug 2018, 13:37 last edited by@raven-worx i will take one example in QT.
enum data{ a,b,c,d }; Q_ENUM(data)
and .cpp file
QMetaEnum e = QMetaEnum::fromType<data>(); for(int i=0;i<=d;i++){ qDebug()<<e.valueToKey(data::a); }
It will print only d(4times). But I want to print a,b,c,d
-
@raven-worx i will take one example in QT.
enum data{ a,b,c,d }; Q_ENUM(data)
and .cpp file
QMetaEnum e = QMetaEnum::fromType<data>(); for(int i=0;i<=d;i++){ qDebug()<<e.valueToKey(data::a); }
It will print only d(4times). But I want to print a,b,c,d
Moderatorswrote on 1 Aug 2018, 14:22 last edited by raven-worx 8 Jan 2018, 14:22@amarism said in QT how to take enum object inside the QList and print accordingly ?:
qDebug()<<e.valueToKey(data::a);
it rather prints 4 times the value of
data::a
since you tell it to do so:qDebug()<<e.valueToKey(data::a);
-
@raven-worx i will take one example in QT.
enum data{ a,b,c,d }; Q_ENUM(data)
and .cpp file
QMetaEnum e = QMetaEnum::fromType<data>(); for(int i=0;i<=d;i++){ qDebug()<<e.valueToKey(data::a); }
It will print only d(4times). But I want to print a,b,c,d
wrote on 1 Aug 2018, 16:09 last edited byfor(int i=0;i<=d;i++){ qDebug()<<e.valueToKey(data::a); }
Like @raven-worx says; you probably want to access that
i
loop counter inside the loop, don't you? :) -
for(int i=0;i<=d;i++){ qDebug()<<e.valueToKey(data::a); }
Like @raven-worx says; you probably want to access that
i
loop counter inside the loop, don't you? :)wrote on 2 Aug 2018, 06:29 last edited byThis post is deleted! -
for(int i=0;i<=d;i++){ qDebug()<<e.valueToKey(static_cast<data>(i)); }
-
@amarism said in QT how to take enum object inside the QList and print accordingly ?:
i tried to take i inside the loop but its show error " 'i': illegal qualified name in member declaration "
QMetaEnum::valueToKey() takes an int parameter, so i don't see why the following shouldn't work.
for(int i=0;i<=d;i++){ qDebug() << e.valueToKey(i); }
-
@amarism said in QT how to take enum object inside the QList and print accordingly ?:
i tried to take i inside the loop but its show error " 'i': illegal qualified name in member declaration "
QMetaEnum::valueToKey() takes an int parameter, so i don't see why the following shouldn't work.
for(int i=0;i<=d;i++){ qDebug() << e.valueToKey(i); }
wrote on 2 Aug 2018, 06:55 last edited by amarism 8 Feb 2018, 06:56@raven-worx ya its working , Thanks a lots
1/14