Qt5 QGraphicsView and OpenGL



  • Hi everybody!

    Qt Version: 5.3 (OpenGL)
    Platform: Windows 7 Pro x64

    I want to use the QGraphicsView object to composite QWidgets over an OpenGL scene. From what I've read in the documentation, I believe this is the simplest program I could write to achieve that:

    @
    class CustomGL : public QGLWidget {
    protected:
    virtual void initializeGL() {}

    virtual void resizeGL(int w, int h) {
    glViewport(0, 0, w, h);
    glClearColor(0.0f, 0.0f, 0.5f, 1.0f);
    }

    virtual void paintGL() {
    glClear(GL_COLOR_BUFFER_BIT);
    }
    };

    int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    QGraphicsScene* pScene = new QGraphicsScene();
    pScene->addLine(0, 0, 400, 400, QPen(QBrush(Qt::green), 5));

    QGraphicsView* pView = new QGraphicsView();
    pView->setViewport(new CustomGL());
    pView->setScene(pScene);
    pView->show();
    return a.exec();
    }
    @

    I would expect a blue background (due to the glClearColor() and glClear() calls) with a green line in the foreground. However, when I run this I get a white background; it does not appear the QGLWidget is visible.

    Next I thought that I would have to subclass QGraphicsScene and draw the from the drawBackground() method, like so:

    @
    class CustomGL : public QGLWidget
    {
    public:
    void paint() {
    paintGL();
    }
    protected:
    // Same initializeGL(), resizeGL(), and paintGL() as above
    };

    class GLScene : public QGraphicsScene
    {
    public:
    GLScene(CustomGL* pGL) : _pGL(pGL) {}

    protected:
    virtual void drawBackground(QPainter* painter, const QRect& rect) {
    _pGL->paint();
    }

    private:
    CustomGL* _pGL;
    };

    int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    GLScene* pScene = new GLScene(new CustomGL());
    pScene->addLine(0, 0, 400, 400, QPen(QBrush(Qt::green), 5));

    QGraphicsView* pView = new QGraphicsView();
    pView->setViewport(new CustomGL());
    pView->setScene(pScene);
    pView->show();
    return a.exec();
    }
    @

    This also fails to produce the expected result; GLScene::drawBackground() is never hit.

    In fact, using this "example code":http://doc.qt.digia.com/qq/qq26-openglcanvas.html, I get the same issue: a white background where the OpenGL scene is suppose to be.

    Can anyone shed some light on this? Is this a Qt4 vs. Qt5 issue?

    Thank you,

    --Chris


Log in to reply
 

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