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