PDA

View Full Version : Drawing widgets inside a Qt application



aughey
13th May 2010, 00:07
I've seen Qt widgets embedded into a QGraphicsView. What I'd like to do is draw QWidgets inside a OpenGL application.

In general, the code will setup the OpenGL scene and transformations, then ask a widget to paint itself using OpenGL. I know there are OpenGL paint engines, I just don't know how to setup the painting devices to have the widget paint inside of a larger OpenGL app.

In practice, this could be done inside of a QGLWidget. In the paintGL routine, it'll do the opengl calls it needs, and at the appropriate time, setup a opengl paint engine and draw the widget, then finish the GL drawing.

Can someone tell me if this is something I can do?

John

wysota
13th May 2010, 00:39
You should create a graphics view with OpenGL viewport (see QGraphicsView::setViewport() - set a QGLWidget as the viewport). Then you will be able to place widgets inside the scene and they will be rendered using OpenGL calls. At the same time you can use OpenGL calls to render i.e. the background of your scene (drawBackground()).

aughey
13th May 2010, 02:24
That's not quite what I'm looking for. I don't want to use QGraphicsViews, I want a widget to draw itself, on command, using the OpenGL calls. It'd be something like this....

void MyObj::paintGL() {
// setup gl matrices

// do other GL painting stuff

QPushButton button("foobar");

button.paint(within_this_established_openGL_enviro nment);

// Manipulate the modelview matricies.

// Draw that same button again transformed just to make a point.
button.paint(within_this_established_openGL_enviro nment);
}

I realize this isn't right, but this is the context in which I want to render widgets.

desertpilot
13th May 2010, 05:45
Can't comment on OpenGL, but usually I create a QWidget inherited object and overwrite the virtual QWidget::paintEvent(QPaintEvent* event):



void MyWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
// use painter to draw
}

Then I place it into an empty QFrame object of the parent window.

wysota
13th May 2010, 08:45
I don't want to use QGraphicsViews, I want a widget to draw itself, on command, using the OpenGL calls.
And what is wrong with using Graphics View for that?

If you just want the looks of a button, then you can render a button to an image (either directly using a widget or through QStyle) and then use that as a texture in OpenGL. But the widget will not be clickable, etc. For that you need Graphics View.