PDA

View Full Version : QMetaEnum



No-Nonsense
3rd February 2007, 12:19
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

wysota
3rd February 2007, 13:12
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.

No-Nonsense
3rd February 2007, 14:58
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

wysota
3rd February 2007, 16:24
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.

No-Nonsense
6th February 2007, 17:09
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:


const QMetaObject metaObject = QDockWidget::staticMetaObject;
const QMetaEnum metaEnum = metaObject.enumerator(metaObject.indexOfEnumerator ("Qt::DockWidgetArea"));
qDebug() "metaObject.isValid() = " << metaObject.isValid(); // false!!!
Qt::DockWidgetArea area = metaEnum.keyToValue("Qt::RightDockWidgetArea"); // -1 (key not found)

Any idea what I am doing wrong?

Thanks in advance,
-Jens

wysota
6th February 2007, 17:25
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...

No-Nonsense
7th February 2007, 16:15
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!


const QMetaObject metaObject = QDockWidget::staticMetaObject;
const QMetaEnum dwaEnum = metaObject.property(metaObject.indexOfProperty("allowedAreas")).enumerator();
Qt::DockWidgetArea defaultArea = (Qt::DockWidgetArea)dwaEnum.keyToValue("Qt::" +
dockWidgetElem.attribute("defaultArea", "RightDockWidgetArea").toAscii());

(dockWidgetElem is a QDomElement)

-Jens