Providing QImage in a QGLWidget QML Plugin

I am trying to write a QML plugin that reads frames from a video (using its own widget to perform this task, not QtMultimedia / Phonon), and each frame is converted to QImage RGB888 and then displayed on a QGLWidget (for performance reasons). Right now, nothing is drawing on the screen, and the screen remains white all the time.

It is important to note that I already have all this without QGLWidget , so I know that the problem is setting up and using QGLWidget.

The plugin is registered using:

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

therefore Video is the main class of the plugin. On this 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 moving on to the canvas object, let me say that I overloaded Video::paint() , so it calls canvas.paint() when passing QImage as a parameter, I don’t know if this is right. so I would like to get 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 title 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 follows:

 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?

I initially asked this question on the Qt Forum , but did not receive any answers.

+4
c ++ qt qt4 opengl qml
source share
2 answers

solvable . The problem was that I was trying to create a new GL context in my plugin when I had to extract the GL context from the loadable application.

This code was very useful to understand how to do this.

By the way, I found that the material was drawing inside the view . I just needed to do view->show() , but this created another window that was not what I was looking for. The link I shared above has an answer.

+5
source share

I think you need to draw your glwidget using opengl functions.

One possible way is to override the paintGL () method in GLWidget and then draw the image using glDrawPixels ().

 glClear(GL_COLOR_BUFFER_BIT); glDrawPixels(buffer.width(), buffer.height(), GL_RGBA, GL_UNSIGNED_BYTE, buffer.bits()); 

Where the buffer is a QImage object that needs to be converted using the static method QGLWidget :: convertrtToGLFormat ().

Take a look at this source for reference: https://github.com/nayyden/ZVector/blob/master/src/GLDebugBufferWidget.cpp

0
source share

All Articles