A Mixture of Qt Jambi and AWT / Swing

A week or two ago, Eskil wrote a blog about how he'd been working on mixing AWT and Qt together in the same window. At the time he'd gotten to the point where we could embed a Qt widget inside an AWT component. Now we've gotten a bit further...

Based on the same approach we used for the Qt inside AWT embedding, we could also do the other way around pretty easy. The result is something along the lines of:

Qt Jambi and AWT

As opposed to Eskil, I decided to not show the picture of us with flowers in our hair that we took in San Francisco when we went for JavaOne earlier this year... What you're seeing here is a QWidget with a grid layout with two QWidgets on the left, a QTextEdit with an html page and the CollidingMice example. On the right is the SwingSet demo. We also tried to embed a Java3D example there and that worked equally well.

The solution is not completely there yet, though. I did spend a day trying to get the focus handling between the two libraries right, and it was surprisingly non-trivial. AWT for instance doesn't pass on the information that you're doing SHIFT-Tab to do backwards traversal of the focus chain, so you need to figure that out based on the component that lost focus and where it is relative to the current one in the focus chain, and so forth... Lots of fun things. Right now its at a state where it works most of the time, but through mouse clicks and back/forward tabbing it breaks after a while so there is still work to be done.

The other issues are window activation, as we're actually embedding the window natively and while the AWT component has focus, Qt becomes inactive, etc, but I'm not sure we'll be able to get around that one. The other issue is OS X where AWT and Qt Jambi cannot run in parallel because of the Carbon event system not being thread safe.

Right now the code to get this example running looks something like this:

import com.trolltech.research.awt.*;
import com.trolltech.qt.gui.*;
import com.trolltech.launcher.*;

public class TestHost {

public static void main(String args[]) {

QWidget root = new QWidget();

QGridLayout layout = new QGridLayout(root);

layout.addWidget(new QTextEdit(), 0, 0);
layout.addWidget(new com.trolltech.examples.CollidingMice(null), 1, 0);

SwingSet2 component = new SwingSet2(null, java.awt.GraphicsEnvironment.

QAwtHost host = new QAwtHost(component);
layout.addWidget(host, 0, 1, 2, 1);;



All in all though, I find this pretty cool as it opens up for people to migrate from AWT / Swing to Qt Jambi and still being able to use thirdparty rendering libraries and the like.

Blog Topics: