[Development] RFC: Improved Q_ENUM

Olivier Goffart olivier at woboq.com
Wed Feb 18 17:44:05 CET 2015


On Wednesday 18 February 2015 11:25:54 Matthew Woehlke wrote:
> 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)?

I have not tried QSettings yet, but since it works with QVariant it should 
work.

> 
> 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?)

Yes, the conversion is case sensitive. It works using QMetaEnum.
In Qt 5.5 you can do
QMetaEnum::fromType<MyObject::MyEnum>().keyToValue("ABCD");
 
> Because I am lazy :-), can you give us a brief summary of how string
> conversion works (or does it?) for flags?

Flags, are not yet supported within QVariant. Maybe for 5.6

-- 
Olivier 

Woboq - Qt services and support - http://woboq.com - http://code.woboq.org



More information about the Development mailing list