[Development] RFC: Improved Q_ENUM

Matthew Woehlke mw_triad at users.sourceforge.net
Wed Feb 18 17:25:54 CET 2015


On 2014-12-15 08:38, Olivier Goffart wrote:
> I have been working on some improvements to moc and the meta type system to 
> improve Q_ENUM. Those changes are targeting Qt 5.5
> [...]
> 
> If you use Q_ENUM, you get:
>  - Using it with qDebug prints the actual string of the value.
>  - a QVariant containing that enum can automatically be converted to QString
>    or QByteArray with the value name. Also, if the QVariant contains a QString
>    or a QByteArray with a value name, it can be converted to the enum type.
>    (This could be useful for QML as well, as you should not need to
>    artificially expose the enums to QML anymore)

Sorry to jump in so late, but... *THIS IS AWESOME!* Converting enums to
strings and vice versa is very common, but I'm not aware of a generic
solution to the problem until now. (Yes, bidirectional; as mentioned,
use in settings, UI, file export etc. are all existing non-debug uses.
Not that the QDebug support isn't killer by itself :-).)

I have two questions, though:

1. How does this interact with QSettings? If I directly pass an enum to
e.g. QSettings::setValue, is it written as an int or an enum (I guess
int)? What about if I use QVariant::fromValue (i.e. so the variant type
is the enum)?

2. If I have a QString that I want to convert to the enum, is there a
way to test if the string matches an enum value? (Also, is the
conversion case sensitive?)

Because I am lazy :-), can you give us a brief summary of how string
conversion works (or does it?) for flags?

-- 
Matthew




More information about the Development mailing list