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. List all the enum object ?
Forum Updated to NodeBB v4.3 + New Features

List all the enum object ?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 5.6k Views 2 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #2

    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

    JonBJ 1 Reply Last reply
    1
    • mrjjM mrjj

      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

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

      @mrjj
      His code is not actually attempting to get the names of the enum values. It needs to know the upper limit of the QStyle::StandardPixmap values, which seems to be QStyle::SP_LineEditClearButton at present.... If that's what he means by his question.

      mrjjM 1 Reply Last reply
      1
      • JonBJ JonB

        @mrjj
        His code is not actually attempting to get the names of the enum values. It needs to know the upper limit of the QStyle::StandardPixmap values, which seems to be QStyle::SP_LineEditClearButton at present.... If that's what he means by his question.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @JNBarchan
        Ah, good catch. I was fooled by the "QStyle::SP_ArrowBack" text.
        F2 is still his friend :)

        JonBJ 2 Replies Last reply
        0
        • mrjjM mrjj

          @JNBarchan
          Ah, good catch. I was fooled by the "QStyle::SP_ArrowBack" text.
          F2 is still his friend :)

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

          @mrjj
          If he's waiting for C++ to tell him the upper "bound" of the enum, I think he's out of luck, unless you know better.

          mrjjM 1 Reply Last reply
          0
          • mrjjM mrjj

            @JNBarchan
            Ah, good catch. I was fooled by the "QStyle::SP_ArrowBack" text.
            F2 is still his friend :)

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

            @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 the for loop. Who knows here... !

            1 Reply Last reply
            0
            • JonBJ JonB

              @mrjj
              If he's waiting for C++ to tell him the upper "bound" of the enum, I think he's out of luck, unless you know better.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

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

              kshegunovK 1 Reply Last reply
              0
              • mrjjM mrjj

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

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #8

                http://doc.qt.io/qt-5/qmetaenum.html

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                4
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Ah, thanks @kshegunov , its flagged with
                  Q_ENUM(StandardPixmap)
                  so one can use Qt meta system.

                  kshegunovK 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    Ah, thanks @kshegunov , its flagged with
                    Q_ENUM(StandardPixmap)
                    so one can use Qt meta system.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by mrjj
                    #10

                    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;
                    

                    Read and abide by the Qt Code of Conduct

                    sonichyS 1 Reply Last reply
                    4
                    • goldenhawkingG Offline
                      goldenhawkingG Offline
                      goldenhawking
                      wrote on last edited by
                      #11

                      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.

                      Qt is the best C++ framework I've ever met.

                      goldenhawkingG 1 Reply Last reply
                      0
                      • goldenhawkingG goldenhawking

                        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.

                        goldenhawkingG Offline
                        goldenhawkingG Offline
                        goldenhawking
                        wrote on last edited by
                        #12

                        @goldenhawking said in List all the enum object ?:

                        meta

                        -Qt's meta compiler makes c++ a "Dynamic" like language , it's very important feature.

                        Qt is the best C++ framework I've ever met.

                        1 Reply Last reply
                        0
                        • kshegunovK kshegunov

                          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;
                          
                          sonichyS Offline
                          sonichyS Offline
                          sonichy
                          wrote on last edited by sonichy
                          #13
                          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);
                          }

                          https://github.com/sonichy

                          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