I have a QFlag that I have created. I would like to use this QFlag in QML. Specifically, I would like to be able to OR together a couple of flags and pass them as a parameter to a method.
I noticed that QFlags are not explicitly listed here as a QML supported data type: http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#supported-data-types
Which types do I need to register or Q_MACROs do I need to use to enable this functionality?
The goal is to have a method call I can use in QML that looks like this:
myObject.setFlag(MyFlagType.A | MyFlagType.C)
My QFlag code:
#include <QFlags>
#include <QObject>
class ColorPickerStyle : public QObject {
Q_OBJECT
public:
enum ColorPickerStyleFlag {
None 0x00,
MSOfficeColors = 0x01,
RGBSlider = 0x02,
ColorWheel = 0x04,
CustomColorSet = 0x08
};
//Create ColorPickerStyle::Flags as a type
Q_DECLARE_FLAGS(Flags, ColorPickerStyleFlag)
//Register ColorPickerStyle::Flags with the meta-type system
Q_FLAGS(Flags)
Q_ENUMS(ColorPickerStyleFlag)
ColorPickerStyle();
virtual ~ColorPickerStyle();
};
//Qt requires lots of macros
Q_DECLARE_OPERATORS_FOR_FLAGS(ColorPickerStyle::Flags)
Additionally declaring
should be enough. Enums are integers, so the or operator would also work without the Q_FLAGS declarations from QML.
The class ColorPickerStyle needs the Q_OBJECT macro as well so that the meta object compiler works correctly.
In the end you can use the values in QML as ColorPickerStyle.None, ColorPickerStyle.MSOfficeColors, …