Subnavigation

Qt Jambi and Enums

If you've looked through the Qt Jambi API, you'll see that we don't
make use of the Java 5 feature of enumerators for mapping Qt/C++
enums. Since we're already using generics, auto-boxing and other Java 5
features it probably seems a bit strange that we don't use enums for
mapping enums in Qt.

The reason in short was: "We didn't see a way how we could..."

Why? Enums in Qt are used in a number of different ways. Some of them
are bit flags that are passed to a QFlags< ...>, some are a list of
special number and some are extensible id's such as QEvent::Type, where
we provide x number of default events and then users can define their
own if they specify the event type to be higher than
QEvent::User. Java enums on the other hand are _only_ enumerators, a
list of named values from 0 to number_of_entries.

We could easily solve the numbered enums used as bitmasks and special
numbers, even a generics based QFlags which made typesafe flags. The
problem of QEvent::Type and the extensible enum was a different
issue. 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.

So from Tech Preview 3 of Qt Jambi you'll start seeing support for real
enums and real QFlags. To give you some idea, here's how it will look:


// Checking for enum values:
switch (event.type()) {
case MouseButtonPress: ...
case MouseButtonRelease: ...
case MouseMove: ...
}


// Custom event:
QEvent.Type myEventType = QEvent.Type.resolve(QEvent.Type.User.value() + 1);


// Creating, setting and unsetting bits in a QFlags
QPainter.RenderHints hints = new QPainter.RenderHints();
hints.set(QPainter.RenderHint.Antialiasing);
hints.set(QPainter.RenderHint.SmoothPixmapTransform);
painter.setRenderHints(hints);
hints.unset(QPainter.RenderHint.SmoothPixmapTransform);
graphicsView.setRenderHints(hints);

That was a small update from the Java team, now its back to working
with the JavaDoc support...


Blog Topics:

Comments