title: Rendering QImage on QGLWidget of QML plugin
body: <p>I'm trying to write a <strong>QML plugin</strong> that reads frames from a video (using a custom widget to do that task, NOT QtMultimedia/Phonon), and each frame is converted to a <code>QImage</code> RGB888, and then displayed on a <code>QGLWidget</code> (for performance reasons). Right now nothing is draw to the screen and the screen stays white all the time.</p>
  
  <p>It's important to state that I already have all of this working without <code>QGLWidget</code>, so I know the issue is setting up and drawing on QGLWidget.</p>
  
  <p>The plugin is being registered with:</p>
  
  <pre><code>qmlRegisterType&amp;lt;Video&amp;gt;(uri,1,0,"Video");
  </code></pre>
  
  <p>so <code>Video</code> is the main class of the plugin. On it's constructor we have:</p>
  
  <pre><code>Video::Video(QDeclarativeItem* parent)
  : QDeclarativeItem(parent), d_ptr(new VideoPrivate(this))
  {    
      setFlag(QGraphicsItem::ItemHasNoContents, false);            
  
      Q_D(Video);
      QDeclarativeView* view = new QDeclarativeView;
      view-&gt;setViewport(&amp;d-&gt;canvas()); // canvas() returns a reference to my custom OpenGL Widget
  }
  </code></pre>
  
  <p>Before I jump to the <code>canvas</code> object, let me say that I overloaded <code>Video::paint()</code> so it calls <code>canvas.paint()</code> while passing <code>QImage</code> as parameter, I don't know if this is the right way to do it so I would like some advice on this:</p>
  
  <pre><code>void Video::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
  {
      Q_UNUSED(painter);
      Q_UNUSED(widget);
      Q_UNUSED(option);
  
      Q_D(Video);
      // I know for sure at this point "d-&gt;image()" is valid, but I'm hiding the code for clarity
      d-&gt;canvas().paint(painter, option, d-&gt;image());
  }
  </code></pre>
  
  <p>The <code>canvas</code> object is declared as <code>GLWidget canvas;</code> and the header of this class is defined as:</p>
  
  <pre><code>class GLWidget : public QGLWidget
  {
      Q_OBJECT
  public:        
      explicit GLWidget(QWidget* parent = NULL);
      ~GLWidget();
  
      void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QImage* image);
  };
  </code></pre>
  
  <p>Seems pretty simple. Now, the implementation of <code>QGLWidget</code> is the following:</p>
  
  <pre><code>GLWidget::GLWidget(QWidget* parent)
  : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  {
     // Should I do something here?
     // Maybe setAutoFillBackground(false); ???
  }
  
  GLWidget::~GLWidget()
  {
  }
  </code></pre>
  
  <p>And finally:</p>
  
  <pre><code>void GLWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QImage* image)
  {
     // I ignore painter because it comes from Video, so I create a new one:
     QPainter gl_painter(this);
  
  
     // Perform drawing as Qt::KeepAspectRatio
  
     gl_painter.fillRect(QRectF(QPoint(0, 0), QSize(this-&gt;width(), this-&gt;height())), Qt::black);
  
     QImage scaled_img = image-&gt;scaled(QSize(this-&gt;width(), this-&gt;height()), _ar, Qt::FastTransformation);
  
     gl_painter.drawImage(qRound(this-&gt;width()/2)  - qRound(scaled_img.size().width()/2),
                          qRound(this-&gt;height()/2) - qRound(scaled_img.size().height()/2),
                          scaled_img); 
  }
  </code></pre>
  
  <p>What am I missing?</p>
  
  <p>I originally asked <a href="http://developer.qt.nokia.com/forums/viewthread/12358/" rel="nofollow">this question on Qt Forum</a> but got no replies.</p>