Important: Please read the Qt Code of Conduct -

[Solved] Rendering QImage on QGLWidget of QML plugin

  • I'm trying to write a QML plugin that reads frames from a Video (using a custom widget and NOT QtMultimedia/Phonon), then each frame is converted to a QImage with format RGB888, and then displayed on a QGLWidget. Right now nothing seems to be draw on the screen (the screen stays white).

    It's important to state that I already have all of this working without QGLWidget, so I know the issue is setting up and drawing on QGLWidget.

    The plugin is being registered with:


    so Video is the main class of the plugin. On it's constructor we have:
    Video::Video(QDeclarativeItem* parent)
    : QDeclarativeItem(parent), d_ptr(new VideoPrivate(this))
    setFlag(QGraphicsItem::ItemHasNoContents, false);

    QDeclarativeView* view = new QDeclarativeView;
    view->setViewport(&d->canvas()); // canvas() returns a reference to my custom OpenGL Widget


    Before I jump to the canvas object, let me say that I overloaded Video::paint() so it calls canvas.paint() while passing QImage as parameter, I don't know if this is the right way to do it so I would like some advice on this:

    @void Video::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)

    // I know for sure at this point "d->image()" is valid, but I'm hiding the code for clarity
    d->canvas().paint(painter, option, d->image());


    The canvas object is declared as GLWidget canvas; and the header of this class is defined as:

    @class GLWidget : public QGLWidget
    explicit GLWidget(QWidget* parent = NULL);

    void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QImage* image);


    Seems pretty simple. Now, the implementation of QGLWidget is the following:
    GLWidget::GLWidget(QWidget* parent)
    : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
    // Should I do something here?
    // Maybe setAutoFillBackground(false); ???


    And finally:

    @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->width(), this->height())), Qt::black);

    QImage scaled_img = image->scaled(QSize(this->width(), this->height()), _ar, Qt::FastTransformation);

    gl_painter.drawImage(qRound(this->width()/2) - qRound(scaled_img.size().width()/2),
    qRound(this->height()/2) - qRound(scaled_img.size().height()/2),

    What am I missing?

  • The answer for this question "is here":

  • Welcome to devnet

    Thanks for sharing the solution. Please mark the title line with [Solved]. This helps others to see that there is a solution available, if they have a similar question.

Log in to reply