Ah, now it works. It was a little bit unclear in the QGLWidget documentation that the Context is really created automatically and that the QGLContext.create() is something that has to be called from the context object constructor so it will only be used if you need to extend the QGLContext class.
Anyway for completeness sake I will post my code of the LWJGL Widget using the code from the
basic rendering tutorial.
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.opengl.QGLFormat;
import com.trolltech.qt.opengl.QGLWidget;
public class LWJGLWidget extends QGLWidget {
private static float angle = 0;
public LWJGLWidget(QWidget parent) {
super(new QGLFormat(), parent);
}
public LWJGLWidget(QGLFormat format, QWidget parent) {
super(format, parent);
}
protected void initializeGL() {
if (this.context().isValid()) {
this.makeCurrent();
try {
GLContext.useContext(this.context());
} catch (LWJGLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
protected void resizeGL(int w, int h) {
// setup viewport, projection etc.:
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0.0, this.width(), 0.0, this.height(), -1.0, 1.0);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glViewport(0, 0, this.width(), this.height());
}
protected void paintGL() {
angle += 2.0f % 360;
// clear the screen
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
// center square according to screen size
GL11.glPushMatrix();
GL11.glTranslatef(this.width() / 2, this.height() / 2, 0.0f);
// rotate square according to angle
GL11.glRotatef(angle, 0, 0, 1.0f);
// render the square
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2i(-50, -50);
GL11.glVertex2i(50, -50);
GL11.glVertex2i(50, 50);
GL11.glVertex2i(-50, 50);
GL11.glEnd();
GL11.glPopMatrix();
}
}
Basically just call makeCurrent() and useContext() in the initializeGL() method (useContext() with the QGLWidget.context()) and use the methods QGLWidget.paintGL() and QGLWidget.resizeGL() as described in the
QGLWidget API to do the rendering and configure the viewport.
Thanks again spasi for providing the admittedly quite simple solution.