简体   繁体   中英

Render QWidget to FBO

I am programming a VR app in Qt and need to show QWidget on the desktop and also inside VR.

So far I am rendering like this:

QOpenGLPaintDevice device(QSize(w12,h12));
QPainter painter(&device);


gl_functions->glBindFramebuffer(GL_FRAMEBUFFER, fbo);

//painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
gl_functions->glClearColor(0.0, 0.0, 0.0, 0.0);
gl_functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

//QPixmap pixmap(widget2->size());
//widget2->render(&pixmap);
//pixmap.save("pic.jpg");


widget2->render(&painter);

This is called from paintGL() of the main QOpenGLWidget.

In Vr this texture is used on a quad. So far only the text gets rendered. If QPixmap is used as device the widget renders fine.

My Widget is very complex with 4k lines of code for connnections between all spinboxes, labels, ...

小部件外观

I found solution, maybe not the best one.

Overlay needs its own context so in init:

overlayGLcontext = new QOpenGLContext();
overlayGLcontext->setFormat( format );
overlayGLcontext->setShareContext(widgetGLcontext);
overlayGLcontext->create();

offscreen_surface = new QOffscreenSurface();
offscreen_surface->create();

...

overlayGLcontext->makeCurrent(offscreen_surface);
fbo = new QOpenGLFramebufferObject( w12, h12, GL_TEXTURE_2D);

...

overlayGLcontext->makeCurrent(offscreen_surface);
fbo->bind();

QOpenGLPaintDevice device(QSize(w12,h12));
QPainter painter(&device);

painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

painter.beginNativePainting();

gl_functions->glClearColor(0.0, 0.0, 0.0, 0.0);
gl_functions->glClear(GL_COLOR_BUFFER_BIT);

painter.endNativePainting();

widget1->render(&painter);
widget2->render(&painter,QPoint(widget1->width(),0),QRegion());

painter.end();

fbo->release();

texture = fbo->texture();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM