Results 1 to 7 of 7

Thread: QMetaEnum

  1. #1
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QMetaEnum

    I am developing an application where some parts of the GUI are loaded dynamically from UI files that are specified inside a configuration file. These widgets are added to the main application as dockwidgets.

    I would like to habe the user specify the default DockWidgetArea inside my configuration file.

    I could use if-elseif to test against the four possible area-strings or a static QMap but I found QMetaEnum and think this would be the way to go?

    But how do I use QMetaEnum? I think I would have to get a QMetaEnum instance from the QMetaObject instance that did define the enum. But the DockWidgetArea enum is defined inside the Qt module. Does the Qt module hava a static QMetaObject?

    I searched the Qt Designer source for metaEnum but that did not help me.

    Thanks in advance,
    -Jens

  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: QMetaEnum

    QMetaObject is associated with a class, not with a "module". Each QObject has a meta object associated with it. Why don't you just use integers? All the enums have integer values, so you can use ints directly. I doubt you'll be able to use QMetaEnum with Qt::DockWidgetArea as it is not a QObject, so it doesn't have a meta object associated with it...

    Edit:
    On the other hand you can get the dock widget meta object, find an index of the appropriate enumerator using indexOfEnumerator and fetch the enumerator object for it. Then you should be able to map between values.
    Last edited by wysota; 3rd February 2007 at 12:22.

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

    No-Nonsense (7th February 2007)

  4. #3
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMetaEnum

    Quote Originally Posted by wysota View Post
    On the other hand you can get the dock widget meta object, find an index of the appropriate enumerator using indexOfEnumerator and fetch the enumerator object for it. Then you should be able to map between values.
    This is what I want to do, but to my understanding QDockWidget would have to declare the enum to make it available in it's (static) QMetaObject using Q_ENUMS. I haven't looked at the QDockWidget source if it does though.

    I will try and see if it works using QDockWidget's QMetaObject.

    The idea to use the meta system of Qt instead of integers is: The user uses strings instead of ints, I do not have to do any range checks as the meta system does this for me and it will still work if something is changed inside Qt. (ok, if i would translate from string to int myself this would be true too.

    -Jens

  5. #4
    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: QMetaEnum

    It has to be declared there because the dock widget class uses it in a property. If it wasn't declared, Designer wouldn't be able to handle it.

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

    No-Nonsense (7th February 2007)

  7. #5
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: QMetaEnum

    Quote Originally Posted by wysota View Post
    It has to be declared there because the dock widget class uses it in a property. If it wasn't declared, Designer wouldn't be able to handle it.
    I looked at the qdockwidget.h and there is no Q_ENUMS declaration... or do you mean it gets declared because it is used in a property?

    I tried:

    Qt Code:
    1. const QMetaObject metaObject = QDockWidget::staticMetaObject;
    2. const QMetaEnum metaEnum = metaObject.enumerator(metaObject.indexOfEnumerator("Qt::DockWidgetArea"));
    3. qDebug() "metaObject.isValid() = " << metaObject.isValid(); // false!!!
    4. Qt::DockWidgetArea area = metaEnum.keyToValue("Qt::RightDockWidgetArea"); // -1 (key not found)
    To copy to clipboard, switch view to plain text mode 

    Any idea what I am doing wrong?

    Thanks in advance,
    -Jens

  8. #6
    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: QMetaEnum

    DockWidgetAreas is registered using Q_FLAGS which includes the same functionality Q_ENUMS has and DockWidgetArea is registered using Q_ENUMS. They're registered from within corelib/global/qnamespace.h.

    Of course there is still a question how to access the data as it's not a part of any QObject subclass...

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

    No-Nonsense (7th February 2007)

  10. #7
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: QMetaEnum

    Quote Originally Posted by wysota View Post
    Of course there is still a question how to access the data as it's not a part of any QObject subclass...
    I did it - looking at abstractformbuilder.cpp. Thanks for your help wysota!

    Qt Code:
    1. const QMetaObject metaObject = QDockWidget::staticMetaObject;
    2. const QMetaEnum dwaEnum = metaObject.property(metaObject.indexOfProperty("allowedAreas")).enumerator();
    3. Qt::DockWidgetArea defaultArea = (Qt::DockWidgetArea)dwaEnum.keyToValue("Qt::" +
    4. dockWidgetElem.attribute("defaultArea", "RightDockWidgetArea").toAscii());
    To copy to clipboard, switch view to plain text mode 

    (dockWidgetElem is a QDomElement)

    -Jens
    Last edited by jacek; 7th February 2007 at 16:00. Reason: wrapped too long line

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.