[Development] Why does QFlag exist? (Not QFlags)

Thiago Macieira thiago.macieira at intel.com
Tue Sep 10 14:17:26 CEST 2024


On Tuesday 10 September 2024 04:56:40 GMT-7 Thiago Macieira wrote:
> In Qt 3, QVariant could only contain a closed set of types, so QObject::
> QObject::setProperty would only be able to write QFlags if the QVariant
> contained an int, and QMetaProperty already had isEnumType() at this time
>   https://doc.qt.io/archives/3.3/qmetaproperty.html#isEnumType
> and moc looked for functions taking or returning integers:
>   https://github.com/gnu-andrew/qt3/blob/master/src/moc/moc.y#L2566-L2586

Qt3-style uint accesses no longer compile:
    enum Enum { E1, E2 };
    Q_ENUM(Enum)
    Q_PROPERTY(Enum enumValue READ enumValue WRITE setEnumValue)
    uint enumValue() const { return e; }
    void setEnumValue(uint v) { e = v; }

error: invalid conversion from ‘uint’ {aka ‘unsigned int’} to 
‘ClassWithEnumAccessAsInteger::Enum’ [-fpermissive]

or

    enum Flag { F1 = 1, F2 = 2 };
    Q_DECLARE_FLAGS(Flags, Flag)
    Q_PROPERTY(Flags flagsValue READ flagsValue WRITE setFlagsValue)
    uint flagsValue() const { return f; }
    void setFlagsValue(uint v) { f = v; }

error: ambiguous overload for ‘operator=’ (operand types are 
‘ClassWithEnumAccessAsInteger::Flags’ {aka 
‘QFlags<ClassWithEnumAccessAsInteger::Flag>’} and ‘uint’ {aka ‘unsigned int’})

But if you add 
    Q_FLAG(Flags)
(not Q_FLAG(Flag))

then it compiles... or did until my changes.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Principal Engineer - Intel DCAI Platform & System Engineering
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5152 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20240910/31d4a774/attachment.bin>


More information about the Development mailing list