[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:

    @qmlRegisterType<Video>(uri,1,0,"Video");@

    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);

    Q_D(Video);
    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)
    {
    Q_UNUSED(painter);
    Q_UNUSED(widget);
    Q_UNUSED(option);

    Q_D(Video);
    // 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
    {
    Q_OBJECT
    public:
    explicit GLWidget(QWidget* parent = NULL);
    ~GLWidget();

    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); ???
    }

    GLWidget::~GLWidget()
    {
    }@

    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),
    scaled_img);
    }@

    What am I missing?



  • The answer for this question "is here":http://stackoverflow.com/a/8488275/176769.


  • Moderators

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.