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

Qt-jambi-interest Archive, October 2006
Extending enums


Message 1 in thread

Hi,

On Gunnar's blog entry about enums, he mentions a way to extend Qt enums in Jambi : 

"Java does not allow the programmer to create enums outside the
enum declaration. Howeverâ After some hours of testing and
prototyping we came up with a means to do this, of course while still
preserving the use of the == operator and the possibility to use enums
in switch statements."

And a code example with :
"QEvent.Type myEventType = QEvent.Type.resolve(QEvent.Type.User.value() + 1);"


I haven't really figured out how this actually works.
AFAICS, resolve() is what creates the user event which will be of type QEvent.Type, am I right ? I wonder how this works, i.e. how can instances of QEvent.Type can be created besides the predefined ones. Also, how is the custom event type used afterwards, e.g. in switch statements ? Can you write :
switch( event.type() )
{
case myEventType: ...
}

Thanks in advance for the enlightenment ;-)
Regards,

Romain Pokrzywka 
Software Engineer
KlarÃlvdalens Datakonsult AB


Message 2 in thread

Romain Pokrzywka wrote:

>I haven't really figured out how this actually works.
>AFAICS, resolve() is what creates the user event which will be of type QEvent.Type, am I right ? I wonder how this works, i.e. how can instances of QEvent.Type can be created besides the predefined ones. Also, how is the custom event type used afterwards, e.g. in switch statements ? Can you write :
>  
>

Hi, Romain.

Enums in Java are basically just classes that are special cased in the 
compiler. For unknown enum values, the resolve() function creates new 
objects of the given enum class. These objects are then cached for later 
references. There is, however, some JNI trickery involved to make this work.

>switch( event.type() )
>{
>case myEventType: ...
>}
>  
>

Unfortunately, enums in switch statements have to be available at 
compile-time, so this code will not compile. However, checking the 
identity of the object will work:

public boolean event(QEvent event)
{
    switch (event.type()) {
       case Show: /* do show event */ break;
       case Hide: /* do hide event */ break;
       default:
           if (event.type() == myEventType) /* do custom event */ break;
    }
}

Hope this helped!

-- Eskil