Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT how to take enum object inside the QList and print accordingly ?
Forum Updated to NodeBB v4.3 + New Features

QT how to take enum object inside the QList and print accordingly ?

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A amarism

    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

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #2

    @amarism

    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);
    

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    A 2 Replies Last reply
    4
    • raven-worxR raven-worx

      @amarism

      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);
      
      A Offline
      A Offline
      amarism
      wrote on last edited by amarism
      #3

      @raven-worx I am getting error when i select the Q_ENUM() below the enum. and my enum is there inside the "namespace"

      raven-worxR 1 Reply Last reply
      0
      • A 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-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #4

        @amarism
        show some code

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        A 1 Reply Last reply
        1
        • raven-worxR raven-worx

          @amarism
          show some code

          A Offline
          A Offline
          amarism
          wrote on last edited by amarism
          #5

          @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-worxR 1 Reply Last reply
          0
          • A amarism

            @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-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #6

            @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
                 ...
            }
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            3
            • raven-worxR raven-worx

              @amarism

              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);
              
              A Offline
              A Offline
              amarism
              wrote on last edited by
              #7

              @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-worxR JonBJ 2 Replies Last reply
              0
              • A amarism

                @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-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by raven-worx
                #8

                @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);

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                4
                • A amarism

                  @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

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #9

                  @amarism

                    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? :)

                  A 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @amarism

                      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? :)

                    A Offline
                    A Offline
                    amarism
                    wrote on last edited by
                    #10
                    This post is deleted!
                    J.HilkJ raven-worxR 2 Replies Last reply
                    0
                    • A amarism

                      This post is deleted!

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #11

                      @amarism

                      for(int i=0;i<=d;i++){
                          qDebug()<<e.valueToKey(static_cast<data>(i));
                       }
                      

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      A 1 Reply Last reply
                      1
                      • A amarism

                        This post is deleted!

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by
                        #12

                        @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);
                         }
                        

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        A 1 Reply Last reply
                        2
                        • raven-worxR raven-worx

                          @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);
                           }
                          
                          A Offline
                          A Offline
                          amarism
                          wrote on last edited by amarism
                          #13

                          @raven-worx ya its working , Thanks a lots

                          1 Reply Last reply
                          0
                          • J.HilkJ J.Hilk

                            @amarism

                            for(int i=0;i<=d;i++){
                                qDebug()<<e.valueToKey(static_cast<data>(i));
                             }
                            
                            A Offline
                            A Offline
                            amarism
                            wrote on last edited by
                            #14

                            @J.Hilk Without QT, Can I do the same thing(by using the normal C++ method like a 2-D array) ??

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved