Results 1 to 2 of 2

Thread: Making enums "setProperty" enabled.

  1. #1
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Making enums "setProperty" enabled.

    Hello,

    The code i'm talking about is part of a library. In this context, and in the following lines, the macro RELibOpt will expand to either Q_DECL_EXPORT or Q_DECL_IMPORT depending on what "end" the header is being included.

    i have a namespace named RE, containing enums declared like this:
    Qt Code:
    1. // This trick is to allow the namespace to act as a class for Qt MetaObject stuff.
    2. #ifdef Q_MOC_RUN
    3. class RELibOpt RE {
    4. // To avoid making a heavy Q_OBJECT for the RE namespace.
    5. Q_GADGET
    6. Q_ENUMS (ReadingDirection /* and other enums... */)
    7. public:
    8. #else
    9. namespace RE {
    10. #endif
    11. // This gets declared in the generated moc... Very handy trick BTW.
    12. extern QMetaObject const staticMetaObject;
    13. // Here comes one of my enum declaration...
    14. enum ReadingDirection {
    15. ReadingDirectionUnknown = 0,
    16. ReadingDirectionLeftToRight,
    17. ReadingDirectionRightToLeft,
    18. ReadingDirectionBottomToTop,
    19. ReadingDirectionTopToBottom
    20. };
    21. // See explaination below for that macro, it doesn't changes anything of the problem anyway.
    22. RegisterEnumStreamingOperators (ReadingDirection);
    23. // This is a macro i made to avoid repeating code for each enum i'd like to stream from / to QDataStream.
    24. #define RegisterEnumStreamingOperators(T) \
    25. static QDataStream& operator<< (QDataStream& s, T const& t) { return s << (qint32)t; }; \
    26. static QDataStream& operator>> (QDataStream& s, T &t) { qint32 q; s >> q; t = (T)q; return s; }
    27. };
    28. Q_DECLARE_METATYPE(RE::ReadingDirection);
    To copy to clipboard, switch view to plain text mode 

    Later on my program (At exec time), i call:
    Qt Code:
    1. qRegisterMetaTypeStreamOperators<RE::ReadingDirection>("RE::ReadingDirection");
    2. qRegisterMetaType<RE::ReadingDirection>("RE::ReadingDirection")
    To copy to clipboard, switch view to plain text mode 

    Then, i have objects using these enums as properties. For instance:
    Qt Code:
    1. class RELibOpt AnObject : public QObject {
    2. Q_OBJECT
    3. Q_PROPERTY (RE::ReadingDirection readingDirection READ readingDirection WRITE setReadingDirection)
    4. public:
    5. // Constructor.
    6. Q_INVOKABLE AnObject (QObject *p=0);
    7. // Accessors.
    8. RE::ReadingDirection const& readingDirection () const { return _readingDirection; }
    9. void setReadingDirection (RE::ReadingDirection const &v) { _readingDirection = v; }
    10. };
    To copy to clipboard, switch view to plain text mode 

    Using any kind of property setter / getter via QObject::property() / QObject::setProperty() works just fine for everything, but not for my enum types. For instance, doing:
    Qt Code:
    1. AnObject *obj = new AnObject(this);
    2. obj->setProperty("readingDirection", RE::ReadingDirectionTopToBottom);
    3. // OR, as specified in the docs:
    4. obj->setProperty("readingDirection", "RE::ReadingDirectionTopToBottom");
    To copy to clipboard, switch view to plain text mode 

    i have noticed that:
    Qt Code:
    1. QVariant v = qVariantFromValue(RE::ReadingDirectionBottomToTop);
    2. qDebug() << v.isValid() << v.canConvert<RE::ReadingDirection>() << v;
    To copy to clipboard, switch view to plain text mode 
    Gives output:
    Qt Code:
    1. true true QVariant(RE::ReadingDirection, )
    To copy to clipboard, switch view to plain text mode 
    In other words, the QVariant appears to be valid, but doesn't contain the right value...

    Any idea what i'm doing wrong here please?
    Thanks for your help,
    Pierre.
    Last edited by hickscorp; 27th July 2011 at 04:04.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Making enums "setProperty" enabled.

    Bases on this:
    If you want to register an enum that is declared in another class, the enum must be fully qualified with the name of the class defining it. In addition, the class defining the enum has to inherit QObject as well as declare the enum using Q_ENUMS().
    I'd say you need to add this:
    Qt Code:
    1. class RELibOpt AnObject : public QObject {
    2. Q_OBJECT
    3. Q_ENUMS(RE::ReadingDirection) //<<----
    4. Q_PROPERTY (RE::ReadingDirection readingDirection READ readingDirection WRITE setReadingDirection)
    5. public:
    6. // Constructor.
    7. Q_INVOKABLE AnObject (QObject *p=0);
    8. // Accessors.
    9. RE::ReadingDirection const& readingDirection () const { return _readingDirection; }
    10. void setReadingDirection (RE::ReadingDirection const &v) { _readingDirection = v; }
    11. };
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 5
    Last Post: 9th May 2011, 14:40
  2. Replies: 1
    Last Post: 7th April 2010, 22:46
  3. Replies: 3
    Last Post: 15th February 2010, 18:27
  4. Replies: 0
    Last Post: 15th November 2009, 10:40
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.