Results 1 to 3 of 3

Thread: Qt Guru Required for MetaObject system problem.

  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 Qt Guru Required for MetaObject system problem.

    Hello,

    i have several questions about the Qt MetaObject system, related to issues i have with an application i'm working on.

    1) Namespace Enums.
    i have a very wide namespace named "RE". To allow declaration of enums and types inside that namespace while still being covered by the Qt MetaObject processing system, i use such trick:
    Qt Code:
    1. #ifdef Q_MOC_RUN
    2. class RELibOpt RE {
    3. Q_GADGET
    4. Q_ENUMS (AnEnum, AnotherEnum)
    5. public:
    6. #else
    7. namespace RE {
    8. #endif
    9. // My whole fake class / namespace comes here,
    10. // including all my enums declarations... Is there another way to make an enum "global" but still part of the parsed meta-object system?...
    11. }
    12.  
    13. Q_DECLARE_METATYPE(RE::AnEnum);
    14. Q_DECLARE_METATYPE(RE::AnotherEnum);
    To copy to clipboard, switch view to plain text mode 
    Is that right to do that? What would be the other solution to have a namespace containing enums and registered typedefs? i would like to be able to have my enums part of my "RE" namespace exactly as the Qt enums are part of the "Qt" namespace... What is Qt in this case? A class? A namespace? How is it processed by MOC?

    2) Properties and Enums.
    Still in my "RE" namespace, i have classes. In those classes, i use the Q_PROPERTY macro. However, if in a class RE::A i use a Q_PROPERTY using RE::AnEnum as it's base type, i'm unable to enumerate it's content with QMetaEnum: the QMetaProperty exists but says it's not an enum type! What is the solution to do that?

    Thanks a lot,
    Pierre.

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

    Default Re: Qt Guru Required for MetaObject system problem.

    There is a special code path in moc for processing the "Qt" namespace (yes, it is a namespace). It is impossible to use that path with user code unless something changed since the last time I analyzed the code (which was a couple of minor versions back). As for the second case I can only tell you to remember Qt meta-type system is based on a character-by-character string comparison. Can you access the enums defined in RE manually by processing its meta object?
    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
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Guru Required for MetaObject system problem.

    Quote Originally Posted by wysota View Post
    There is a special code path in moc for processing the "Qt" namespace (yes, it is a namespace). It is impossible to use that path with user code unless something changed since the last time I analyzed the code (which was a couple of minor versions back).
    Ok, thanks for the precision. As of the special way to process the Qt namespace, that's exactly what i'm doing with that preprocessor trick (Which emulates a class at MOC'ing time to allow the staticMetaObject to be generated).

    Quote Originally Posted by wysota View Post
    As for the second case I can only tell you to remember Qt meta-type system is based on a character-by-character string comparison. Can you access the enums defined in RE manually by processing its meta object?
    i sure can:
    Qt Code:
    1. QMetaObject const &mo = RE::staticMetaObject;
    2. int idx = mo.indexOfEnumerator("ReadingDirection");
    3. QMetaEnum me = mo.enumerator(idx);
    4. RE::ReadingDirection key = RE::ReadingDirectionRightToLeft;
    5. qDebug() << key << "to string:" << me.valueToKey(key);
    6. QString sz = "RE::ReadingDirectionRightToLeft";
    7. qDebug() << sz << "to value:" << me.keyToValue(sz.toLatin1());
    To copy to clipboard, switch view to plain text mode 
    Output (Which is right):
    2 to string: ReadingDirectionRightToLeft
    "RE::ReadingDirectionRightToLeft" to value: 2

    But still, i can't seem to access any enums on my QMetaProperties from my other classes:
    Qt Code:
    1. QMetaObject const &mo = RE::MyClass::staticMetaObject;
    2. int mpIdx = mo.indexOfProperty("readingDirection");
    3. QMetaProperty const &mp = mo.property(mpIdx);
    4. qDebug() << "Index:" << mpIdx << "isEnum" << mp.isEnumType();
    To copy to clipboard, switch view to plain text mode 
    Output (Which is obviously wrong, the isEnum should be true!):
    Index: 5 isEnum false
    For this example, i have tried all kinds of Q_PROPERTY declarations on my MyClass class... Ones with the RE:: specified on the base type, ones without, mixed, or not... It won't change the fact that the property is not seen as an enum:
    Qt Code:
    1. Q_PROPERTY(ReadingDirection readingDirection READ readingDirection WRITE setReadingDirection)
    2. Q_PROPERTY(RE::ReadingDirection readingDirection READ readingDirection WRITE setReadingDirection)
    3. // none of them works.
    To copy to clipboard, switch view to plain text mode 

    Any clue please?


    Added after 15 minutes:


    This is so weird. i'm actually looking at qabstractscrollarea.h. In it, i can see:
    Qt Code:
    1. class Q_GUI_EXPORT QAbstractScrollArea : public QFrame {
    2. Q_OBJECT
    3. Q_PROPERTY(Qt::ScrollBarPolicy verticalScrollBarPolicy READ verticalScrollBarPolicy WRITE setVerticalScrollBarPolicy)
    4. // Rest of the declaration.
    5. }
    To copy to clipboard, switch view to plain text mode 
    This is in essence strictly identical to my stuff. Even the namespacing, since the QT_NAMESPACE could expand to something different than global namespace:
    Qt Code:
    1. namespace RE {
    2. class RELibOpt MyClass : public QObject {
    3. Q_OBJECT
    4. Q_PROPERTY(RE::ReadingDirection readingDirection READ readingDirection WRITE setReadingDirection)
    5. // Rest of the declaration.
    6. }
    To copy to clipboard, switch view to plain text mode 

    However, this snippet:
    Qt Code:
    1. {
    2. QMetaObject const &mo = RE::MyClass::staticMetaObject;
    3. int mpIdx = mo.indexOfProperty("readingDirection");
    4. QMetaProperty const &mp = mo.property(mpIdx);
    5. qDebug() << "Index:" << mpIdx << "isEnum" << mp.isEnumType();
    6. } {
    7. QMetaObject const &mo = QAbstractScrollArea::staticMetaObject;
    8. int mpIdx = mo.indexOfProperty("verticalScrollBarPolicy");
    9. QMetaProperty const &mp = mo.property(mpIdx);
    10. qDebug() << "Index:" << mpIdx << "isEnum" << mp.isEnumType();
    11. }
    To copy to clipboard, switch view to plain text mode 
    gives the following output:
    Index: 5 isEnum false
    Index: 64 isEnum true

    This is odd... i'm not doing anything different, but i'm getting really different results. What could be the clue?


    Added after 26 minutes:


    One mode addition i discovered. If i add this to the MyClass class:
    Qt Code:
    1. Q_PROPERTY(Qt::ScrollBarPolicy scrollPolicy READ scrollPolicy WRITE setScrollPolicy)
    To copy to clipboard, switch view to plain text mode 
    Then the QMetaProperty marks it's an enum, as opposed to my RE::* enums... Which is even more odd. So the problem definitely comes from the way my RE:: namespace enums gets MOC'ed... Can anyone help please?


    Added after 4 minutes:


    Half a dozen refactorings later, and as many tests later too... i have a very simple question.
    - Let's say we make a class A inheriting publically from QObject, and having the Q_OBJECT macro for good.
    - A also declares an enum AnEnum, and registers it via Q_ENUM.
    - Now lets say there is another class B, with a Q_PROPERTY using A::AnEnum as it's base type.
    1) Would it work?
    2) Now picture the exact same scenario, but both A and B would be part of a namespace named NS. Would this still work?

    i'm unable to make it work with very basic Qt code. Can someone please try and give feedback, and if it works post a snippet?
    Thanks a lot for your time!
    Pierre.
    Last edited by hickscorp; 5th August 2011 at 14:32.

Similar Threads

  1. QObject::MetaObject()
    By pkj in forum Qt Programming
    Replies: 3
    Last Post: 4th June 2011, 10:41
  2. guru, novice, Beginner, Greenhorn, ...
    By john_god in forum General Discussion
    Replies: 1
    Last Post: 11th February 2009, 06:53
  3. system tray problem
    By vvdounai in forum Qt Programming
    Replies: 5
    Last Post: 14th November 2007, 08:25
  4. Help wanted from opencvlibrary Guru WebCam image... to avi..
    By patrik08 in forum General Programming
    Replies: 2
    Last Post: 18th May 2007, 00:52
  5. Example code and metaObject question
    By bruccutler in forum Newbie
    Replies: 1
    Last Post: 12th January 2007, 18:34

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.