Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] Rendering QImage on QGLWidget of QML plugin
Forum Updated to NodeBB v4.3 + New Features

[Solved] Rendering QImage on QGLWidget of QML plugin

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 4.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    karlphillip
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      karlphillip
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        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.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved