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. Converting Enums to Strings for QPrinter

Converting Enums to Strings for QPrinter

Scheduled Pinned Locked Moved Unsolved General and Desktop
qprinterenumqt4qmetaobject
13 Posts 4 Posters 5.9k Views
  • 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.
  • D Offline
    D Offline
    DougyDrumz
    wrote on 27 Jan 2016, 22:29 last edited by
    #4

    Looks fascinating, but I'm having a hard time seeing the forest for the trees. Do you have sample code which calls this macro?

    Dougy Drumz

    K W 2 Replies Last reply 28 Jan 2016, 07:49
    0
    • D DougyDrumz
      27 Jan 2016, 22:29

      Looks fascinating, but I'm having a hard time seeing the forest for the trees. Do you have sample code which calls this macro?

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 28 Jan 2016, 07:49 last edited by kshegunov
      #5

      @DougyDrumz
      Maybe try something simpler than generating classes with macros? This should suffice:

      // These two statics you could also put in your class if you wish (it'd be better even)
      static QPair<EnumType, QString> nameTable[] = {
          qMakePair(EnumValue1, QStringLiteral("EnumValue1")),
          qMakePair(EnumValue2, QStringLiteral("EnumValue2"))
      };
      static const qint32 EnumTypeValuesNumber = 2;
      
      class MyClass
      {
          QHash<EnumType, QString> enumToNameHash;
          QHash<QString, EnumType> nameToEnumHash;
      
          QString enumToString(EnumType enumValue)
          {
              if (enumToNameHash.isEmpty())  {
                  // Lazy init the hash table
                  enumToNameHash.reserve(EnumTypeValuesNumber);
                  for (qint32 i = 0; i < EnumTypeValuesNumber; i++)
                      enumToNameHash.insert(nameTable[i].first, nameTable[i].second);
              }
      
              return enumToNameHash.value(enumValue);
          }
      
          EnumType stringToEnum(QString enumName)
          {
              if (nameToEnumHash.isEmpty())  {
                  // Lazy init the hash table
                  nameToEnumHash.reserve(EnumTypeValuesNumber);
                  for (qint32 i = 0; i < EnumTypeValuesNumber; i++)
                      nameToEnumHash.insert(nameTable[i].second, nameTable[i].first);
              }
      
              return nameToEnumHash.value(enumName);
          }
      
          // ...
      };
      

      As a side note, you could also simplify it a bit if your enums are ordered and are starting from 0. Then one of the hash tables isn't needed. Additionally, you could put one value at the end of the enum to serve as the number of values constant instead of creating its own variable, e.g:

      enum MyEnumType  {
          EnumValue1 = 0, EnumValue2, EnumTypeValuesNumber
      }
      

      Kind regards.

      Read and abide by the Qt Code of Conduct

      W 1 Reply Last reply 28 Jan 2016, 18:33
      0
      • D Offline
        D Offline
        DougyDrumz
        wrote on 28 Jan 2016, 15:51 last edited by
        #6

        I'm a little confused here. What both of you appear to be doing is making a mapping between an enum value and a string representing that value when a class is created. Please correct me if I'm wrong. What I want to do is extract the string representation of an enum value for a class that already exists - in my case QPrinter. The staticMetaObject is a great paradigm for this. Unfortunately there is no staticMetaObject for QPrinter. Do I need to parse the OPrinter header file and grab the strings there? I've seen examples similar to that around.

        I was hoping for some simple magic, but as we know, there is no magic.

        Dougy Drumz

        K 1 Reply Last reply 28 Jan 2016, 16:52
        0
        • D DougyDrumz
          28 Jan 2016, 15:51

          I'm a little confused here. What both of you appear to be doing is making a mapping between an enum value and a string representing that value when a class is created. Please correct me if I'm wrong. What I want to do is extract the string representation of an enum value for a class that already exists - in my case QPrinter. The staticMetaObject is a great paradigm for this. Unfortunately there is no staticMetaObject for QPrinter. Do I need to parse the OPrinter header file and grab the strings there? I've seen examples similar to that around.

          I was hoping for some simple magic, but as we know, there is no magic.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 28 Jan 2016, 16:52 last edited by
          #7

          @DougyDrumz
          Well, I'm not aware of any other way ... an enum is an integer wrapped in a nice language construct, nothing else. In the end the moc will do it in similar fashion, that's why you need to use all that macros for the meta-object system ...

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • D DougyDrumz
            27 Jan 2016, 22:29

            Looks fascinating, but I'm having a hard time seeing the forest for the trees. Do you have sample code which calls this macro?

            W Offline
            W Offline
            Wilk
            wrote on 28 Jan 2016, 18:29 last edited by
            #8

            @DougyDrumz Sorry for making you wait: yesterday forum was down right after your question, and today I've just got to computer.

            // Definition of enum class
            DECLARE_ENUM_TYPE (
            	DatasetElementType,
            	int8_t,
            	uint8_t,
            	int16_t,
            	uint16_t,
            	int32_t,
            	uint32_t,
            	int64_t,
            	uint64_t,
            	float_t,
            	double_t
            )
            

            Then you can use it anewhere in your code:

            DatasetElementType_t value (DatasetElementType_t::uint8_t);
            
            qDebug () << value; // Output: uint8_t
            
            value = DatasetElementType ("uint16_t"); // Create by enum element name, for example, during deserialization.
            
            qDebug () << value; // Output: uint16_t
            

            I've undestud that you want to convert to string and vise versa values from existing enum. Ofcourse, the solution that I've proposed is not exactly the thing you want. However, you may try to implement something like this to be able to wrap QPrinter enum. I don't know any other sane solution for this problem.

            1 Reply Last reply
            0
            • K kshegunov
              28 Jan 2016, 07:49

              @DougyDrumz
              Maybe try something simpler than generating classes with macros? This should suffice:

              // These two statics you could also put in your class if you wish (it'd be better even)
              static QPair<EnumType, QString> nameTable[] = {
                  qMakePair(EnumValue1, QStringLiteral("EnumValue1")),
                  qMakePair(EnumValue2, QStringLiteral("EnumValue2"))
              };
              static const qint32 EnumTypeValuesNumber = 2;
              
              class MyClass
              {
                  QHash<EnumType, QString> enumToNameHash;
                  QHash<QString, EnumType> nameToEnumHash;
              
                  QString enumToString(EnumType enumValue)
                  {
                      if (enumToNameHash.isEmpty())  {
                          // Lazy init the hash table
                          enumToNameHash.reserve(EnumTypeValuesNumber);
                          for (qint32 i = 0; i < EnumTypeValuesNumber; i++)
                              enumToNameHash.insert(nameTable[i].first, nameTable[i].second);
                      }
              
                      return enumToNameHash.value(enumValue);
                  }
              
                  EnumType stringToEnum(QString enumName)
                  {
                      if (nameToEnumHash.isEmpty())  {
                          // Lazy init the hash table
                          nameToEnumHash.reserve(EnumTypeValuesNumber);
                          for (qint32 i = 0; i < EnumTypeValuesNumber; i++)
                              nameToEnumHash.insert(nameTable[i].second, nameTable[i].first);
                      }
              
                      return nameToEnumHash.value(enumName);
                  }
              
                  // ...
              };
              

              As a side note, you could also simplify it a bit if your enums are ordered and are starting from 0. Then one of the hash tables isn't needed. Additionally, you could put one value at the end of the enum to serve as the number of values constant instead of creating its own variable, e.g:

              enum MyEnumType  {
                  EnumValue1 = 0, EnumValue2, EnumTypeValuesNumber
              }
              

              Kind regards.

              W Offline
              W Offline
              Wilk
              wrote on 28 Jan 2016, 18:33 last edited by
              #9

              @kshegunov Hi.
              Basicaly you are wrong and my solution is much easier. The reason for this is pretty simple: proposed macros may be created and debuged once and then anyone may use it for as much as he wants. And addition of new values in enum does not require to change some other code to support it - in you solution you have to edit nameTable every time you add enum members. However my solution lacks ability to create enums where values start not from 0.

              K 1 Reply Last reply 28 Jan 2016, 18:52
              0
              • W Wilk
                28 Jan 2016, 18:33

                @kshegunov Hi.
                Basicaly you are wrong and my solution is much easier. The reason for this is pretty simple: proposed macros may be created and debuged once and then anyone may use it for as much as he wants. And addition of new values in enum does not require to change some other code to support it - in you solution you have to edit nameTable every time you add enum members. However my solution lacks ability to create enums where values start not from 0.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 28 Jan 2016, 18:52 last edited by
                #10

                @Wilk

                Basicaly you are wrong and my solution is much easier. The reason for this is pretty simple: proposed macros may be created and debuged once and then anyone may use it for as much as he wants.

                Surely, why bother with functions if you can use macros ...

                Read and abide by the Qt Code of Conduct

                W 1 Reply Last reply 28 Jan 2016, 19:17
                0
                • K kshegunov
                  28 Jan 2016, 18:52

                  @Wilk

                  Basicaly you are wrong and my solution is much easier. The reason for this is pretty simple: proposed macros may be created and debuged once and then anyone may use it for as much as he wants.

                  Surely, why bother with functions if you can use macros ...

                  W Offline
                  W Offline
                  Wilk
                  wrote on 28 Jan 2016, 19:17 last edited by
                  #11

                  @kshegunov
                  I don't understand you. Macros is a powerfull tool which can be reliable is used in a right way. Just take a look at Qt itself and you will find out quite a lot of macros.

                  K 1 Reply Last reply 28 Jan 2016, 19:27
                  0
                  • W Wilk
                    28 Jan 2016, 19:17

                    @kshegunov
                    I don't understand you. Macros is a powerfull tool which can be reliable is used in a right way. Just take a look at Qt itself and you will find out quite a lot of macros.

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 28 Jan 2016, 19:27 last edited by
                    #12

                    @Wilk
                    This is beside the scope of this thread and to be honest I don't want to enter in fruitless discussions. Whatever the reason is that you think using macros is best in this case I don't have a problem with, in any case it's your choice how you will do your coding.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DougyDrumz
                      wrote on 28 Jan 2016, 19:48 last edited by
                      #13

                      Let me tone down this discussion by saying that the original reason for my question has disappeared. I misunderstood how QPrinter worked. I thought I would need the string versions of the QPrintDialog enums to pass on to lp. Turns out QPrinter does what I need to do automatically and I don't need to call lp. Thanks for the lively discussion though.

                      Dougy Drumz

                      1 Reply Last reply
                      0

                      13/13

                      28 Jan 2016, 19:48

                      • Login

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