View Full Version : QGLWidget renderText rotation

20th September 2006, 23:44
I am trying to rotate some text for a 2-D plot I am making. I am trying to put this in a portion of my left viewport so that it runs from bottom to top. The bottom of the letters will face the inside of the plot. I am designing this program in Qt and using a QGLWidget. Here is the snipet of my code I have:

glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
glOrtho( -10.0, 10.0, -10.0, 10.0, -10.0, 10.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
glViewport( ( leftMarginWidth / 4.0 ), 0.0, ( leftMarginWidth / 4.0 ), winHeight );
glScissor( ( leftMarginWidth / 4.0 ), 0.0, ( leftMarginWidth / 4.0 ), winHeight );
mpQgl->qglColor( mLeftLabelColor );
glPushMatrix( );
mpQgl->renderText( 0.0, 0.0, 0.0, mLeftLabel );
glRotated( 90.0, 0.0, 0.0, 1.0 );
glPopMatrix( );

mLeftLabel is a QString of what holds the label.
leftMarginWidth is a double holding the width of the left margin. I allow for a title to be in the first 1/4 of the margin, followed by the label in the 2nd quarter of the margin, followed by the actual axis tick marks and corresponding values in the 2nd half of the margin.
mpQgl is a pointer to the QGLWidget I am drawing in.
I have 3 different viewports on each margin and margins on all 4 sides and then the viewport for the plot in the middle. So the only viewport I want rotated is the viewport with the axis label on the left side. I have tried moving the rotate before the render text and also moving the renderText outside the push/pop matrix, but nothing helps. The text always renders in a horizontal manner inside the middle of the viewport I created. Here is a link to renderText (http://doc.trolltech.com/4.1/qglwidget.html#renderText-2).

Thanks for all your help in trying to help me figure out why my text isn't rotating. If you need more information, please let me know what it is I am missing so I can try to find what you need to further help me.

26th September 2006, 14:25
Does anybody have any ideas whether renderText actually rotates the text or keeps the text in a horizontal position but just moves the test with the object? I still have not been able to figure this out. Thanks!

27th September 2006, 17:35
Since I cannot figure how to get renderText to rotate the text I tried to use a QPainter to draw my rotated text. Here is the code I use:

mpLabelPainter = new QPainter( );
mpLabelPainter->begin( mpQgl );
mpLabelPainter->setClipping( true );
mpLabelPainter->setMatrixEnabled( true );
mpLabelPainter->setWindow( 0, 0, winWidth, winHeight );
mpLabelPainter->setViewport( QRect( ( leftMarginWidth / 4.0 ), 0.0, ( leftMarginWidth / 4.0 ), winHeight ) );
mpLabelPainter->setClipRect( QRect( ( leftMarginWidth / 4.0 ), 0.0, ( leftMarginWidth / 4.0 ), winHeight ), Qt::ReplaceClip );
QMatrix matrix;
matrix.rotate( 270.0 );
mpLabelPainter->setMatrix( matrix, true );
mpLabelPainter->setFont( textFont );
mpLabelPainter->setPen( Qt::black );
mpLabelPainter->drawText( QPointF( 0.0, 0.0 ), mLeftLabel );
mpLabelPainter->end( );

mpQgl is a pointer to my QGLWidget.
leftMarginWidth is the width of my left margin. I want to render this text inside the 2nd quarter of my left margin which is why I start the rectangle at 1/4 the way over and the width is only 1/4 of the margin width.
winWidth and winHeight are the height and width of the QGLWidget.
textFont is the font used in the program.
mLeftLabel is the QString I want to write to the screen.

When I run this code, the 2nd half of my margin turns to a white block. Why is the QPainter overwritingn this area when I set its clip and viewport in the 2nd quarter of the margin, not the 2nd half. The block turns white when I just call the new QPainter, begin, and end lines. If I comment out every other line but those three, I get the white rectangle in the wrong spot. If I comment out all the code, then nothing behaves incorrectly. I should also mention that text is never being drawn on any combination of lines of code. Does it look as if something is working wrong? Thanks for your help in trying to help me fix this!

19th September 2007, 18:52
I also tried to rotate the text and couldn't. Here is what I found out:

You cannot use rotate and renderText and it is by design and has historical reasons.

You do need to use QPainter, apply a transformation, and use the QPainter::drawText function to do it.

However, you need to do these things:
1) you need to call setAutoFillBackground(false) on the QGLWidget so that QPainter::begin() won't call glClear().
2) you need to turn off setAutoBufferSwap so that QPainter::end() won't swap the buffers
3) you need to avoid doing any GL calls in between QPainter::begin and QPainter::end.

I haven't had a chance to try it yet. Hope this works for you.

19th September 2007, 21:52
About 10 months ago I worked on a bitmap text in which I just figured out how to render the letters at a rotated angle so it would work in my plot. But these ideas are great as I would love to use the QPainter instead of my bitmap text to render onto the plots at sometime. Thanks for the pointers and maybe I will try them out sometime soon.