Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, January 2008
Re: Q_ENUMS: howto register 'external' enums


Message 1 in thread

This does not work for me in (Qt 4.3).

I have a lot of enumerated types defined in a non-QObject base class
(not a namespace)
that I want to multiply inherit in other classes.  Some of these will
indirectly inherit from QObject themselves, so I don't want my
QiGlobal class to inherit also, causing multiple inheritance from
qobject.

But I looked at the qnamespace.h and thought I'd try out that trick of
conditionally including the Q_OBJECT and Q_ENUM macro only from moc. I
enclosed the qobject macros in conditional compilation directives:

class QiGlobal {
#ifdef Q_MOC_RUN
    Q_OBJECT
    Q_ENUMS(QiWideKeyAction)
    Q_ENUMS(QiCellDecorationStyle)
    Q_ENUMS(QiCopyPolicy)
    Q_ENUMS(QiClearPolicy)
    Q_ENUMS(QiSortMode)
#endif
 /// all my enums definitions go here

}

But when I try to build it, I get:

Error: Class contains Q_OBJECT macro but does not inherit from QObject.

Why does it work for qnamespace but not for me? I'm not sure. Why does
Qt forbid this anyway? I can imagine many situations where one might
want to use the Q_ENUMS to define a few enumerators in a non-QObject
class that one can use for inheritance in other classes.

Is there a straightforward way to locate enumerators in one place that
is not a QObject class, but instead just a regular class that one can
multiply inherit from? That would make these Q_ENUMS so much more
useful.


On Jul 31, 2007 5:37 AM, Richard Moore <richmoore44@xxxxxxxxx> wrote:
> On 7/31/07, veronique.lefrere@xxxxxx <veronique.lefrere@xxxxxx> wrote:
> >  maybe it's stupid but... why don't you do, like in qt  qnamespace.h ?
> > you register and declare you enums and just include this .h in class file
> > you want to use them.
>
> Qt uses some magic to make this work:
>
> #ifndef Q_MOC_RUN
> namespace
> #else
> class Q_CORE_EXPORT
> #endif
> Qt {
> #ifdef Q_MOC_RUN
>     Q_OBJECT
>     Q_ENUMS(Orientation TextFormat BackgroundMode DateFormat
> ScrollBarPolicy FocusPolicy ContextMenuPolicy CaseSensitivi
> ty LayoutDirection ArrowType ShortcutContext)
>
> Looking at this appears that sometimes the namespace is treated as a
> namespace, and other times as class. I don't see any reason why this
> shouldn't work though it seems a bit evil.
>
> Cheers
>
> Rich.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>

--
 [ signature omitted ] 

Message 2 in thread

> > Looking at this appears that sometimes the namespace is treated as a
> > namespace, and other times as class. I don't see any reason why this
> > shouldn't work though it seems a bit evil.

Why don't you do the same with your class, i.e. in the normal case have
QiGlobal be your custom class,
but in the Q_MOC_RUN case, have it inherit from QObject.

Cheers,
Peter

--
 [ signature omitted ] 

Message 3 in thread

I think you can use Q_GADGET instead of Q_OBJECT to get it to let you  
use Q_ENUMS without inheriting QObject.

Katrina

Le 25 janv. 08 à 18:05, Alan Ezust a écrit :

> This does not work for me in (Qt 4.3).
>
> I have a lot of enumerated types defined in a non-QObject base class
> (not a namespace)
> that I want to multiply inherit in other classes.  Some of these will
> indirectly inherit from QObject themselves, so I don't want my
> QiGlobal class to inherit also, causing multiple inheritance from
> qobject.
>
> But I looked at the qnamespace.h and thought I'd try out that trick of
> conditionally including the Q_OBJECT and Q_ENUM macro only from moc. I
> enclosed the qobject macros in conditional compilation directives:
>
> class QiGlobal {
> #ifdef Q_MOC_RUN
>     Q_OBJECT
>     Q_ENUMS(QiWideKeyAction)
>     Q_ENUMS(QiCellDecorationStyle)
>     Q_ENUMS(QiCopyPolicy)
>     Q_ENUMS(QiClearPolicy)
>     Q_ENUMS(QiSortMode)
> #endif
>  /// all my enums definitions go here
>
> }
>
> But when I try to build it, I get:
>
> Error: Class contains Q_OBJECT macro but does not inherit from  
> QObject.
>
> Why does it work for qnamespace but not for me? I'm not sure. Why does
> Qt forbid this anyway? I can imagine many situations where one might
> want to use the Q_ENUMS to define a few enumerators in a non-QObject
> class that one can use for inheritance in other classes.
>
> Is there a straightforward way to locate enumerators in one place that
> is not a QObject class, but instead just a regular class that one can
> multiply inherit from? That would make these Q_ENUMS so much more
> useful.
>
>
> On Jul 31, 2007 5:37 AM, Richard Moore <richmoore44@xxxxxxxxx> wrote:
>> On 7/31/07, veronique.lefrere@xxxxxx <veronique.lefrere@xxxxxx>  
>> wrote:
>>>  maybe it's stupid but... why don't you do, like in qt   
>>> qnamespace.h ?
>>> you register and declare you enums and just include this .h in  
>>> class file
>>> you want to use them.
>>
>> Qt uses some magic to make this work:
>>
>> #ifndef Q_MOC_RUN
>> namespace
>> #else
>> class Q_CORE_EXPORT
>> #endif
>> Qt {
>> #ifdef Q_MOC_RUN
>>     Q_OBJECT
>>     Q_ENUMS(Orientation TextFormat BackgroundMode DateFormat
>> ScrollBarPolicy FocusPolicy ContextMenuPolicy CaseSensitivi
>> ty LayoutDirection ArrowType ShortcutContext)
>>
>> Looking at this appears that sometimes the namespace is treated as a
>> namespace, and other times as class. I don't see any reason why this
>> shouldn't work though it seems a bit evil.
>>
>> Cheers
>>
>> Rich.
>>
>> --
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx  
>> with "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>>
>>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx  
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>

--
 [ signature omitted ]