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. Get String representation of all QMetaType::Type or QVariant::Type
Forum Updated to NodeBB v4.3 + New Features

Get String representation of all QMetaType::Type or QVariant::Type

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 533 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.
  • InTheBeningingI Offline
    InTheBeningingI Offline
    InTheBeninging
    wrote on last edited by InTheBeninging
    #1

    As both enums aren't Q_ENUMs creating a QMetaEnum::fromType<QMetaType::Type>() won't work. Is there any other automated way to get all Types as QStrings? Or do i have to make a Map myself?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you thinking about the QMetaType::name function ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • InTheBeningingI Offline
        InTheBeningingI Offline
        InTheBeninging
        wrote on last edited by InTheBeninging
        #3

        Yes but an automated way to get ALL enum - string pairs in a container.

        For Q_ENUMs you can utilize QMetaEnum

        enum Colour {
            red = 1,
            green = 2,
            blue = 3
        }
        Q_ENUM(Numbers)
        
        QMap<Colour, QString> colourNames;
        
        QMetaEnum metaEnum = QMetaEnum::fromType<Colour>();
        for(int i = 0; i < metaEnum.keyCount(); i++)
            colourNames.insert(metaEnum.value(i), metaEnum.key(i));
        

        But the QMetaType::Type Enum is not a Q_ENUM, so QMetaEnum can't be used here.

        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          Why do you want that? What will you do with it?

          QMetaType::Type is not a Q_ENUM but even it it was it wouldn't contain the user registered QMetaTypes.
          If for an obscure reason you do want to get ~all QMetaType, you could use the fact that QMetaType use an integer type id and loop over it.

          Something like the following:

            for (int typeId = 1, consecutiveUnregisteredTypes = 0;
                 typeId <= std::numeric_limits<int>::max() && consecutiveUnregisteredTypes < 1000;
                 ++typeId) {
              if (!QMetaType::isRegistered(typeId)) {
                ++consecutiveUnregisteredTypes;
                continue;
              }
              consecutiveUnregisteredTypes = 0;
              qDebug() << typeId << QMetaType(typeId).name();
            };
          
          1 Reply Last reply
          2
          • InTheBeningingI Offline
            InTheBeningingI Offline
            InTheBeninging
            wrote on last edited by
            #5

            I want to add any QVariant type to a QTableWidget Cell. For this im using a QLineEdit QString and a QComboBox to determine which QVariant type the QString should be converted into. To populate my QComboBox i need the variants name to id pairs.
            I've just made a small map manually to add types that can be converted from QString per default. But wouldn't it be nice to list all types, even user types?

            Iterating over all possible numbers does work, but i dont think its efficient, because there are large gaps inbetween them. Qt 6 introduced types with IDs starting at 0x1000 or 0x2000.
            I've digged a bit into how QT generates the enum. It uses a macro which one could use to populate a map by introducing a new macro.

            #define METATYPE_ID_STRING_PAIR(TypeName, Id, Name) \
                {Id, #TypeName},
            
            QMap<int, QString> map = {
                QT_FOR_EACH_STATIC_TYPE(METATYPE_ID_STRING_PAIR)
            };
            

            The UserTypes are stored in a QList so for them one could just use a loop again.

            for (int typeId = QMetaType::User;; typeId++) {
                if (!QMetaType::isRegistered(typeId))
                    break;
                qDebug() << typeId << QMetaType(typeId).name();
             };
            
            1 Reply Last reply
            0
            • InTheBeningingI InTheBeninging has marked this topic as solved on
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

              For Qt's own enum values you could use a library like https://github.com/Neargye/magic_enum or https://github.com/willwray/enum_reflect to get the strings for the enum values.

              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