Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter and QGLWidget using Qt5.1
Forum Updated to NodeBB v4.3 + New Features

QPainter and QGLWidget using Qt5.1

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tilsitt
    wrote on last edited by
    #1

    Hi,

    I am experiencing an issue while porting my soft from Qt4.8 to Qt5.1. In my QGLWidget, I was drawing a QImage as background using a QPainter with the following code:
    @
    MyGLWidget::MyGLWidget(const QGLFormat& _format, QWidget* _parent, Qt::WindowFlags _flags):
    QGLWidget(_format, NULL, _flags)
    {
    setAutoFillBackground(false);
    setAutoBufferSwap(false);
    }

    void MyGLWidget::glDraw()
    {
    QPainter painter(this);

    QImage background;
    // filling background
    painter.setBrush(QBrush(background));
    
    painter.drawRect(0, 0, width(), height());
    
    painter.end();
    
    QGLWidget::glDraw();
    
    swapBuffers();
    

    }
    @

    It was fine with Qt4.8, but fails with Qt5.1. The background remains black, and QPainter::drawRect() generates a GL_INVALID_OPERATION error. Am I missing something?

    1 Reply Last reply
    0
    • N Offline
      N Offline
      NicuPopescu
      wrote on last edited by
      #2

      Hi,

      I think if you use QPainter this way you should reimplement GLWidget::paintEvent(QPaintEvent *event)
      have a look at "Overpainting Example":qthelp://com.trolltech.qt/qdoc/opengl-overpainting.html

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tilsitt
        wrote on last edited by
        #3

        Hi and thanks for your answer. I looked back in the source code for QGLWidget::paintEvent(), which does the following:
        @
        void QGLWidget::paintEvent(QPaintEvent *)
        {
        if (updatesEnabled()) {
        glDraw();
        updateOverlayGL();
        }
        }
        @

        Unfortunately reimplementing paintEvent() instead of glDraw() will not change anything. In fact I chose glDraw() because paintEvent() isn't called when using updateGL() (if you look at the overpainting example, the method animate() causes repaint using update() and not updateGL()). glDraw() is always called, regardless you're using update() or updateGL().

        1 Reply Last reply
        0
        • N Offline
          N Offline
          NicuPopescu
          wrote on last edited by
          #4

          why you put glDraw in paintEvent? the code should be separated, what must be redrawn by calling explicitly updateGL from what is always painted by paintEvent ...

          btw, you can set a background by simply calling gl functions:

          i.e.
          @void GLDrawer::ClearFrameBuffer()
          {
          qDebug()<<"GLDrawer::ClearFrameBuffer";
          glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
          swapBuffers();
          }@

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tilsitt
            wrote on last edited by
            #5

            I didn't put glDraw() in paintEvent(). This isn't my code, this is paintEvent() implementation of QGLWidget. This is the source code of Qt.

            1 Reply Last reply
            0
            • N Offline
              N Offline
              NicuPopescu
              wrote on last edited by
              #6

              ah ok, I have not seen :) ... brr

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved