Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt5 QGraphicsView and OpenGL
Forum Updated to NodeBB v4.3 + New Features

Qt5 QGraphicsView and OpenGL

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.5k 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.
  • F Offline
    F Offline
    flyncode
    wrote on last edited by
    #1

    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

    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