[Interest] Qt 5.1.1 render with both - OpenGL and QPainter - in one window?

Thomas Meyer public at meyer-thomas.com
Wed Sep 25 14:14:48 CEST 2013


Thanks,
that's all in the example, which can found in the Qt Creator examples.
But how can I bring both together?
It is simple to render only with the 'QPainter' in the example.
I have only to overwrite
...

virtual  void  render(QPainter  *painter);

...


Your postulation is in the 'OpenGLWindow' class:
...
classOpenGLWindow:publicQWindow,protectedQOpenGLFunctions

{

     Q_OBJECT

public:

     explicit  OpenGLWindow(QWindow  *parent  =  0);

     ~OpenGLWindow();

     virtual  void  render(QPainter  *painter);

     virtual  void  render();

...

void  OpenGLWindow::render(QPainter  *painter)
{
     Q_UNUSED(painter);
}

void  OpenGLWindow::initialize()
{
}

void  OpenGLWindow::render()
{
     if  (!m_device)
         m_device  =  new  QOpenGLPaintDevice;

     glClear(GL_COLOR_BUFFER_BIT  |  GL_DEPTH_BUFFER_BIT  |  GL_STENCIL_BUFFER_BIT);
     m_device->setSize(size());

     QPainter  painter(m_device);
     render(&painter);
}

...
voidOpenGLWindow::renderNow()

{

     if  (!isExposed())

         return;

     bool  needsInitialize  =  false;

     if  (!m_context)  {

         m_context  =  new  QOpenGLContext(this);

         m_context->setFormat(requestedFormat());

         m_context->create();

         needsInitialize  =  true;

     }

     m_context->makeCurrent(this);

     if  (needsInitialize)  {

         initializeOpenGLFunctions();

         initialize();

     }

     render();

     m_context->swapBuffers(this);

     if  (m_animating)

         renderLater();

}

...

The 'render()' function is overwritten in the 'main' class.
I have replaced the 'Q_UNUSED(painter);' with:
...
painter->setPen(Qt::white);
painter->setFont(QFont("Arial",30));
painter->drawText(100,100,"Triangle");
...
And in the 'main' class I have inserted:
...
voidTriangleWindow::render(void)

{

     const  qreal  retinaScale  =  devicePixelRatio();

     glViewport(0,

                0,

                width()  *  retinaScale,

                height()  *  retinaScale);

     glClear(GL_COLOR_BUFFER_BIT);

     OpenGLWindow::render();

...

It works, but there are some white dots under the word (please see image).
(The triangle is rotating.)
So, I think, I'm making something wrong.

Thanks,
Thomas


Am 25.09.2013 10:07, schrieb Sletta Gunnar:
> This is very possible :)
>
> The classes you are looking for are:
>
> QWindow - to create the window
> QOpenGLContext - to get the OpenGL context
> QOpenGLPaintDevice - to make the opengl context renderable with QPainter
> QPainter - to draw the text.
>
> cheers,
> Gunnar
>
>
> On Sep 24, 2013, at 4:15 PM, Thomas Meyer <public at meyer-thomas.com> wrote:
>
>> Hi,
>> is it possible to use OpenGL and QPainter in one window (Qt 5.1.1)?
>> And if it is possible, how and is it recommended?
>> Example:
>> I want to render the word "Triangle" into the 'OpenGL Window Example'
>> (please see Qt Creator - Welcome - Examples).
>>
>> My first guess is, that it is not possible. I can only render with OpenGL or
>> with QPainter, like the example shows.
>>
>> Thanks,
>>      Thomas
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130925/a6d7d20a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenGL Example.png
Type: image/png
Size: 56616 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130925/a6d7d20a/attachment.png>


More information about the Interest mailing list