袁书豪
袁书豪

Reputation: 23

implement OpenGL commend in Qt QGraphicsItem

I reimplement QGraphicsItem::paint, and use opengl to paint. But the opengl commend is not work. No any error. I don't know why.

#include <QtOpenGL>
#include <qwidget.h>
#include <qgraphicsitem.h>
#include <qpainter.h>
#include <qdebug.h>

class ImageGraphicsItem : public QGraphicsItem
{
public:

QRectF boundingRect(void) const
{
    return QRectF(0, 0, 100, 100);
}

void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
    qDebug() << "gl run";
    painter->fillRect(0,0,128,128,Qt::green);
    painter->beginNativePainting();

    glEnable(GL_SCISSOR_TEST);
    glScissor(0, 0, 64, 64);

    glClearColor(1, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glDisable(GL_SCISSOR_TEST);

    glEnd();


    painter->endNativePainting();
}

private:

};

The ui is followed:

enter image description here enter image description here

Upvotes: 2

Views: 651

Answers (1)

tunglt
tunglt

Reputation: 1072

Make sure that the viewport is a QGLWidget in order to do OpenGL rendering in our graphics scene, here is an example :

    QGraphicsView view(&scene);
    view.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));

Upvotes: 2

Related Questions