Results 1 to 4 of 4

Thread: Use QFlags

  1. #1
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Use QFlags

    Hi all,

    How can I use QFlags?

    I have always used C++ enum.

    Can help me with an example?

    Regards.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Use QFlags

    At its heart it still is a C++ enum, just with a wrapper. You use it much like you use a simple integer except that it enforces type safety.

    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. struct Test {
    5. enum MyFlag {
    6. A = 0x01,
    7. B = 0x02,
    8. C = 0x04,
    9. D = 0X80,
    10. // combinations
    11. AB = A | B
    12. };
    13. Q_DECLARE_FLAGS(MyFlags, MyFlag)
    14.  
    15. enum AnotherEnum { Q = 0x01 };
    16. };
    17. Q_DECLARE_OPERATORS_FOR_FLAGS(Test::MyFlags)
    18.  
    19.  
    20. int main(int argc, char **argv)
    21. {
    22. QCoreApplication app(argc, argv);
    23.  
    24. Test::MyFlags f;
    25. qDebug() << f;
    26.  
    27. f |= Test::D;
    28. qDebug() << f;
    29.  
    30. f = Test::A | Test::B;
    31. qDebug() << f;
    32. qDebug() << f.testFlag(Test::B) << f.testFlag(Test::C);
    33.  
    34. f = Test::AB;
    35. qDebug() << f;
    36. qDebug() << f.testFlag(Test::B) << f.testFlag(Test::C);
    37.  
    38. f &= ~Test::B; // turn off B
    39. qDebug() << f;
    40.  
    41. f = Test::B;
    42. Test::MyFlags g(Test::A | Test::C);
    43. f |= g;
    44. qDebug() << f;
    45.  
    46. // These won't compile: type safety
    47. // f = 0x01;
    48. // f |= 0x08;
    49. // f = Test::Q;
    50.  
    51. // It is not perfect protection but you have to force it
    52. f |= QFlag(0x40); // invalid value
    53. qDebug() << f;
    54.  
    55. return 0;
    56. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use QFlags

    Thanks!

    I have written this code:

    Qt Code:
    1. namespace Test
    2. {
    3.  
    4. enum TState {
    5. ENone = 0x0,
    6. EPause = 0x1,
    7. EPlay = 0x2
    8. };
    9.  
    10. Q_DECLARE_FLAGS(TStates, TState)
    11.  
    12. }
    13.  
    14. Q_DECLARE_OPERATORS_FOR_FLAGS( Test::TStates )
    15.  
    16. class myClass
    17. {
    18. ...
    19. Test::TStates getState(){return state;}
    20. ...
    21. Test::TStates state;
    To copy to clipboard, switch view to plain text mode 

    ¿How can I declare a QFlag inside the myClass class? Can you help me with an example?

    Regards!

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Use QFlags

    Exactly as you have except the flag enum values should be powers of 2
    Qt Code:
    1. namespace Test {
    2. enum MyFlag {
    3. A = 0x01, // powers of two like this
    4. B = 0x02,
    5. C = 0x04,
    6. D = 0X08,
    7. E = 1 << 8, // or you can do it like this
    8. F = 1 << 9,
    9. G = 1 << 10,
    10. H = 1 << 11
    11. };
    12. Q_DECLARE_FLAGS(MyFlags, MyFlag)
    13. }
    14. Q_DECLARE_OPERATORS_FOR_FLAGS(Test::MyFlags)
    15.  
    16. class MyClass
    17. {
    18. public:
    19. Test::MyFlags someFunc() { return flags; }
    20. private:
    21. Test::MyFlags flags;
    22. };
    To copy to clipboard, switch view to plain text mode 
    Compiles fine.

Similar Threads

  1. Replies: 5
    Last Post: 18th July 2010, 08:39
  2. QFlags questions
    By elcuco in forum Newbie
    Replies: 3
    Last Post: 19th March 2010, 14:33
  3. Storing and Retrieving a QFlags
    By darkadept in forum Qt Programming
    Replies: 3
    Last Post: 4th October 2007, 18:53

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.