Results 1 to 5 of 5

Thread: How to send enum through network?

  1. #1
    Join Date
    Oct 2010
    Location
    Poland
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to send enum through network?

    I need send an enum through network. I have idea how to do this, but I want to know yours solutions for this. Maybye there is better way than my.

    My idea:
    Qt Code:
    1. // h
    2. Q_PROPERTY( Operation operation READ operation WRITE setOperation )
    3. Q_ENUMS( Operation );
    4.  
    5. enum Operation
    6. {
    7. A,
    8. B
    9. };
    10.  
    11. // cpp
    12. QDataStrem in( &tcpSocket );
    13. // waiting for all data
    14. QString sTmpTypOperacji;
    15. Operation eTypOperacji;
    16.  
    17. in >> sTmpTypOperacji;
    18.  
    19. const QMetaObject* metaObj = this->metaObject();
    20. int iEnum = metaObj->indexOfEnumerator( "Operation" );
    21. QMetaEnum enumOper = metaObj->enumerator( iEnum );
    22.  
    23. eTypOperacji = (Operation)enumOper.keyToValue( sTmpTypOperacji.toLatin1() );
    To copy to clipboard, switch view to plain text mode 
    Are there other ways to send enum?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to send enum through network?

    The enum you have is just a list of numbers.
    A = 0
    B = 1
    etc...

    use qDebug() << "My enum =" << theEnum; to see the value

    Just send the number via your network. On the other end cast it back to the enum again.

  3. #3
    Join Date
    Oct 2010
    Location
    Poland
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send enum through network?

    Thanks for reply. I have another question. Enum is qint32 type or other? Int could be a different size on different os so when I read int which represent enum, so which type of int will be safe?

  4. #4
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send enum through network?

    Quote Originally Posted by tbscope View Post
    The enum you have is just a list of numbers.
    A = 0
    B = 1
    etc...

    use qDebug() << "My enum =" << theEnum; to see the value

    Just send the number via your network. On the other end cast it back to the enum again.
    No, don't. The size of an enum is undefined. Instead, pick a portable type (such as quint8) and use that instead.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to send enum through network?

    Just cast the enum to a quint32 and send that through the network. If your using that enum constantly, then you might want to investigate more to determine the minimum and maximum values, but you would then have to ensure that it doesn't overflow in the future, so quint32 would be a better option.

Similar Threads

  1. Replies: 6
    Last Post: 10th October 2010, 23:15
  2. How to export enum ?
    By Peppy in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2010, 15:15
  3. How can we convert enum to int?
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2010, 16:08
  4. Send QImage over network
    By winston2020 in forum Qt Programming
    Replies: 5
    Last Post: 20th March 2009, 19:09
  5. Send QEvents over network?
    By Winni in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2007, 20:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.