Qt OpenGL Rendering text issue QGLWidget

I use QGLWidget and this code draws text on the screen, but rendering is catastrophic if the line length is too long: text rendering issue

Here is my code:

glPushMatrix(); glRotatef(90, 0, 0, 1); QString qStr = QString("Here a very long string which doesn't mean anything at all but had some rendering problems"); renderText(0.0, 0.0, 0.0, qStr); glPopMatrix(); 
+4
source share
2 answers

I had the same problem when using Helvetica . Changing the font to Arial enabled it.

I did a little wrapper around to make things easier:

 void _draw_text(double x, double y, double z, QString txt) { glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); qglColor(Qt::white); renderText(x, y, z, txt, QFont("Arial", 12, QFont::Bold, false) ); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); } 
+3
source

From the documentation:

This function can only be used inside the QPainter :: beginNativePainting () / QPainter :: endNativePainting () block, if the default OpenGL paint engine is QPaintEngine :: OpenGL. To force QPaintEngine :: OpenGL to use the default GL mechanism, call QGL :: setPreferredPaintEngine (QPaintEngine :: OpenGL) before the QApplication constructor.

Therefore, did you try to use QPainter::beginNativePainting() just before the call and QPainter::endNativePainting() right after?

Also note that the text is displayed in the window coordinate, not taking into account your current state of the OpenGL matrix (in short, your call to glRotatef(90, 0, 0, 1) does not affect). You can see in here to save the current state of OpenGL by calling qt_save_gl_state() , then create their new matrices with:

 glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, width, height); glOrtho(0, width, height, 0, 0, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); 

Then draw the text and finally restore the previous OpenGL state with qt_restore_gl_state()

0
source

All Articles