January 28, 2011

cazador7907 cazador7907
Lab Rat
78 posts

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.

//Main

  1.     QGraphicsScene scene;
  2.     scene.setSceneRect(0,0, 800, 600);
  3.     scene.setItemIndexMethod(QGraphicsScene::NoIndex);
  4.     scene.addWidget(new BackGroundGLWidget());
  5.  
  6.     QGraphicsView view(&scene);
  7.     view.setMaximumSize(800, 600);
  8.     view.setAlignment(Qt::AlignCenter);
  9.     view.showMaximized();
  10.     view.show();

PaintGL Code

  1.     // Clear screen and depth buffer
  2.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  3.  
  4.     glLoadIdentity(); // Reset view matrix
  5.     glTranslatef(0.0f, 0.0f, -1.0f); // Move into screen 1 unit
  6.  
  7.     // Pulsing colors based on text position
  8.     glColor3f(qMax(0.0, 1.0f * cos(cnt1)),
  9.               qMax(0.0, 1.0 * sin(cnt2)),
  10.               qMin(1.0, 1.0f - 0.5f * cos(cnt1 + cnt2)));
  11.  
  12.     // Print text on the screen
  13.     QFont font;
  14.     font.setBold(true);
  15.     font.setPixelSize(18);
  16.  
  17.     // Print some text without taking the transformation into account
  18.     // Very useful class to align some text in OpenGL at the absolute
  19.     // borders of the screen!
  20.     QFontMetrics metrics(font);
  21.     QString bottomRightText("I am always at the bottom right.");
  22.     renderText((width() - metrics.width(bottomRightText) - 10)/2,
  23.                (height() - metrics.height() + 10)/2, bottomRightText, font);

 Signature 

Laurence -

 

5 replies

January 29, 2011

Franzk Franzk
Lab Rat
830 posts

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

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

January 29, 2011

peppe peppe
Ant Farmer
1025 posts

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

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

January 29, 2011

cazador7907 cazador7907
Lab Rat
78 posts

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

Laurence –

 Signature 

Laurence -

 

January 29, 2011

Franzk Franzk
Lab Rat
830 posts

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

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

January 29, 2011

peppe peppe
Ant Farmer
1025 posts

cazador7907 wrote:

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?

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

 
  ‹‹ Usage of QSharedPointer and QVector. Problem with references.      ui passing widget to class causes segmentation fault ››

You must log in to post a reply. Not a member yet? Register here!