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. [SOLVED] QDataStream operator >> read to an enum
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QDataStream operator >> read to an enum

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 15.1k 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.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by
    #2

    You probably should overload the input operator QDataSream.

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ibecker
      wrote on last edited by
      #3

      That is actually what I am doing. I simplified a bit for the above example though. Here's what I'm doing for the overload:

      @
      QDataStream& operator >>(QDataStream& in, exampleEnum& e)
      {
      in >> ((quint32)&e); //Anything wrong with this? It certainly feels dirty

      //This also works, but I would love to one-line it
      quint32 buffer;
      in >> buffer;
      e = exampleEnum(buffer);
      
      return in;
      

      }
      @

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jake007
        wrote on last edited by
        #4

        Why don't you only cast enum to int
        @exampleEnum a = First;
        qDebug() << (int)a; // Outputs 0
        @

        Same goes with QDataStream.

        And you work with integer here on.
        You can also cast back from int to enums the same way.

        Or I'm missing the point here ;-) .

        Regards,
        Jake


        Code is poetry

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #5

          @
          QDataStream& operator >>(QDataStream& in, exampleEnum& e)
          {
          in >> ((quint32)&e); //Anything wrong with this? It certainly feels dirty
          return in;
          }
          @
          or
          @
          QDataStream& operator >>(QDataStream& in, exampleEnum& e)
          {
          //This also works, but I would love to one-line it
          quint32 buffer;
          in >> buffer;
          e = exampleEnum(buffer);

          return in;
          

          }
          @

          I personally would prefer the second version as long as it does not consume too much CPU because called often. Gut feelings tell me that the first might be dangerous.

          Just guessing not tried. How about this:
          @
          QDataStream& operator >>(QDataStream& in, exampleEnum& e)
          {
          quint32 &tmp = e;
          in >> tmp;
          return in;
          }
          @
          Possibly the compiler doesn't complain.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ibecker
            wrote on last edited by
            #6

            [quote author="Jake007" date="1330543911"]Why don't you only cast enum to int
            @exampleEnum a = First;
            qDebug() << (int)a; // Outputs 0
            @

            Same goes with QDataStream.

            And you work with integer here on.
            You can also cast back from int to enums the same way.

            Or I'm I missing the point here ;-) .

            Regards,
            Jake[/quote]
            As I noted, there is no problem writing to the QDataStream (likewise qDebug) because it automatically casts to int.

            The trouble is that it does not know what to do when reading. I understand why it can't do it without overloading the operator.
            What I would really like to do is something like this:
            @
            QDataStream& operator >>(QDataStream& in, exampleEnum& e)
            {
            in >> (quint32)e;
            return in;
            }
            @
            But for some reason, it won't convert to quint32.

            The situation is that I'm receiving datagrams and converting them to structures. I don't think that detail is particularly relevant though to my question.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              ibecker
              wrote on last edited by
              #7

              [quote author="koahnig" date="1330544180"]@
              QDataStream& operator >>(QDataStream& in, exampleEnum& e)
              {
              quint32 &tmp = e;
              in >> tmp;
              return in;
              }
              @
              Possibly the compiler doesn't complain. [/quote]

              Well, this works. It also caused me to realize why I couldn't cast to quint32. Because it is a reference to exampleEnum, not just the value. facepalm

              This works:
              @
              QDataStream& operator >>(QDataStream& in, exampleEnum& e)
              {
              in >> (quint32&)e;
              return in;
              }
              @

              Thanks!

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #8

                You are right. facepalm. I did not think far enough either. Getting tired and time to call it a day. :-)

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jake007
                  wrote on last edited by
                  #9

                  Please mark thread as solved :) .

                  Regards,
                  Jake


                  Code is poetry

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    ibecker
                    wrote on last edited by
                    #10

                    Did I do it correctly? Just changed the topic.
                    [quote author="Jake007" date="1330546312"]Please mark thread as solved :) .

                    Regards,
                    Jake[/quote]

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #11

                      That was fine. However, most are putting it to the start of the line. I have changed for you.

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        ibecker
                        wrote on last edited by
                        #12

                        Thanks and duly noted.
                        [quote author="koahnig" date="1330546759"]That was fine. However, most are putting it to the start of the line. I have changed for you.[/quote]

                        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