Important: Please read the Qt Code of Conduct -

QGLWidget and QGraphicsScene Program Crash

  • I've been experimenting with rendering a QGLWidget within a QGraphicsScene. The program compiles just fine. However, when I attempt to render some text to the middle of the widget, the program will run for about a second and then it crashes. I've narrowed the problem down to the paintGL method (code below). However, I can't figure out why it's crashing.

    QGraphicsScene scene;
    scene.setSceneRect(0,0, 800, 600);
    scene.addWidget(new BackGroundGLWidget());

    QGraphicsView view(&scene);
    view.setMaximumSize(800, 600);


    PaintGL Code
    // Clear screen and depth buffer

    glLoadIdentity(); // Reset view matrix
    glTranslatef(0.0f, 0.0f, -1.0f); // Move into screen 1 unit
    // Pulsing colors based on text position
    glColor3f(qMax(0.0, 1.0f * cos(cnt1)),
              qMax(0.0, 1.0 * sin(cnt2)),
              qMin(1.0, 1.0f - 0.5f * cos(cnt1 + cnt2)));
    // Print text on the screen
    QFont font;
    // Print some text without taking the transformation into account
    // Very useful class to align some text in OpenGL at the absolute
    // borders of the screen!
    QFontMetrics metrics(font);
    QString bottomRightText("I am always at the bottom right.");
    renderText((width() - metrics.width(bottomRightText) - 10)/2,
               (height() - metrics.height() + 10)/2, bottomRightText, font);


  • renderText is buggy at best (and about to be obsoleted). Try removing that call and see whether it crashes...

  • You can't put a QGLWidget inside a QGraphicsScene because it paints on screen.

  • So, how would I set a jpg image as overlay dialogs, buttons, lines and the like?

    Laurence -

  • Hm, you should do setViewport(new QGLWidget) on your graphics view. That enables you to do opengl stuff in your scene.

  • [quote author="cazador7907" date="1296309277"]
    So, how would I set a jpg image as overlay dialogs, buttons, lines and the like?

    Laurence -


    I'm not getting it... do you have all of that in a QGraphicsScene or as ordinary widgets? Where and how is OpenGL involved in this?

Log in to reply