Results 1 to 4 of 4

Thread: QFlags questions

  1. #1
    Join Date
    Jan 2006
    Posts
    371
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QFlags questions

    This is my fist time using QFlags and something is not working here. I am probably missing something very stupid and I need a second eye...

    testing-flags.cpp

    Edit:
    code is simple enough to put inline as well...

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QFlags>
    3. #include <QDebug>
    4.  
    5. class foobar{
    6. public:
    7. enum MyFlag{
    8. flag1, flag2, flag3
    9. };
    10. Q_DECLARE_FLAGS(MyFlags, MyFlag)
    11.  
    12. MyFlags m_state;
    13.  
    14. void setFlag( MyFlag f, bool enable){
    15. if (enable)
    16. m_state &= !f;
    17. else
    18. m_state |= f;
    19. }
    20.  
    21. void toggleFlag( MyFlag f ){
    22. setFlag( f, !m_state.testFlag(f) );
    23. }
    24.  
    25. bool getFlag( MyFlag f){
    26. return m_state.testFlag(f);
    27. }
    28. };
    29.  
    30. int main()
    31. {
    32. foobar f;
    33. qDebug() << "Flags state" << f.getFlag( foobar::flag1 );
    34. f.toggleFlag(foobar::flag1);
    35. qDebug() << "Flags state" << f.getFlag( foobar::flag1 );
    36. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFlags questions

    I think the enum should have proper values (i.e. powers of 2 - 1, 2, 4, 8, etc.) so that flags don't interfere with each other. You might also want Q_DECLARE_OPERATORS_FOR_FLAGS if something doesn't compile.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Posts
    371
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFlags questions

    'Ok, new version. Now I get the correct value (false) by default. This was done by the new default values of flags. still, after "toggle", I don't see the flags changing state:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QFlags>
    3. #include <QDebug>
    4.  
    5. class foobar{
    6. public:
    7. enum MyFlag{
    8. flag1 = 1,
    9. flag2 = 2,
    10. flag3 = 4
    11. };
    12. Q_DECLARE_FLAGS(MyFlags, MyFlag)
    13.  
    14. MyFlags m_state;
    15.  
    16. void setFlag( MyFlag f, bool enable){
    17. if (enable)
    18. m_state &= !f;
    19. else
    20. m_state |= f;
    21. }
    22.  
    23. void toggleFlag( MyFlag f ){
    24. setFlag( f, !m_state.testFlag(f) );
    25. }
    26.  
    27. bool getFlag( MyFlag f){
    28. return m_state.testFlag(f);
    29. }
    30. };
    31. Q_DECLARE_OPERATORS_FOR_FLAGS(foobar::MyFlags)
    32.  
    33.  
    34. int main()
    35. {
    36. foobar f;
    37. qDebug() << "Flags state" << f.getFlag( foobar::flag1 );
    38. f.toggleFlag(foobar::flag1);
    39. qDebug() << "Flags state" << f.getFlag( foobar::flag1 );
    40. }
    To copy to clipboard, switch view to plain text mode 

    This is a run:
    Qt Code:
    1. [elcuco@pinky ~/src/qt4-bugs/testing-flags] make && ./testing-flags
    2. g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o testing-flags.o testing-flags.cpp
    3. g++ -Wl,-O1 -o testing-flags testing-flags.o -lQtGui -lQtCore -lpthread
    4. Flags state false
    5. Flags state false
    To copy to clipboard, switch view to plain text mode 

    edit:
    arg.ssf?!#@ ?!@$!@$ .... there is a bug in the "toggle" method, this is why it was not working. Finding and fixing it is left to the reader as en exercise. Thanks wysota!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QFlags questions

    Lol.... ! != ~
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    elcuco (30th May 2010)

Similar Threads

  1. New to QT and have a few questions.
    By vbman213 in forum Newbie
    Replies: 3
    Last Post: 1st February 2010, 06:50
  2. Some questions for Qt?
    By tszzp in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2009, 09:46
  3. Storing and Retrieving a QFlags
    By darkadept in forum Qt Programming
    Replies: 3
    Last Post: 4th October 2007, 18:53
  4. A few questions about Qt/Win
    By Bojan in forum Installation and Deployment
    Replies: 3
    Last Post: 16th January 2006, 09:54

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.