PDA

View Full Version : QPainter::drawText - nothing drawn



Vit Stepanek
12th November 2010, 15:48
Hi,
I'm trying to draw a background grid with the text on the QGraphicsView. The grid is fine, but I can't make it to draw a text. I've played with fonts, brushes etc., setting different values there but the text doesn't appear. There's probably something silly, but I've no more ideas. Does anybody see what's wrong on this?



void DgChartView::drawBackground( QPainter* painter, const QRectF& rect )
{
painter->drawText( rect.center(), "SOME TEXT SOME TEXT" );

for ( QVector<GridData>::iterator it = m_bgGridVert.begin( ); it < m_bgGridVert.end( ); it++ )
{
GridData& data = *it;
QLineF line( QPointF( data.pos, rect.top( ) ), QPointF( data.pos, rect.bottom( ) ) );
painter->drawLine( line );
}

}


Once again, the lines drawn in the loop are displayed correctly, but no text (here's just an example of drawing to the center of the visible rect).

Thanks,
Vit

high_flyer
12th November 2010, 16:03
Hmm... try explicitly setting a valid pen to the painter, or check the font metrics.
Also, it is a good practice to use painter->save() and painter->restore() at the beginning and end of paint helper functions.

Vit Stepanek
12th November 2010, 19:51
Thanks, I'm a bit closer now.
Using QFontMetrics you had suggested I got the size of the text (QFontMetrics::size). I used the received rect to draw it on the view. There I've found one thing - the area for a text is scaled according to the scale matrix applied on the scene. I add here a picture where it's obvious (the scene is scaled largely in X direction and a little in Y). The small rectangle in the center is the one.
However, no text, even if the scale is reset.
Maybe would help to draw the text in view coordinates (not scaled by a view matrix). After all, the text should keep the same size independently on the current scale.
May I ask for a hint please?



Also, it is a good practice to use painter->save() and painter->restore() at the beginning and end of paint helper functions.

Sharing of good practice techniques is never enough, thanks!

Current code;


QString str( "TEXT TEXT" );
painter->save();
QFont font( "Helvetica" );
font.setPointSize( 64 );
painter->setFont( font );
QFontMetrics fMetrics = painter->fontMetrics();
QSize sz = fMetrics.size( Qt::TextSingleLine, str );
QRectF txtRect( rect.center(), sz );
painter->drawText( txtRect, Qt::TextSingleLine, str );
painter->drawRect( txtRect );
...
painter->restore();

high_flyer
14th November 2010, 15:40
Why don't you just a QGraphicsTextItem?

Vit Stepanek
23rd November 2010, 09:42
Hi, I come with the solution finally.
First, the painter is saved (QPainter::save), then the point where to draw a text is translated to the view coordinates and the matrix is disabled. Then the text is drawn correctly. Finally, the painter is restored again.



painter->save();
QPoint viewCoord = mapFromScene( QPoint( data.pos, rect.bottom() ) );
painter->setMatrixEnabled(false);
painter->drawText( viewCoord, data.description );
painter->restore();

wysota
23rd November 2010, 10:59
You would have really saved yourself the trouble by using a text item with ItemIgnoresTransformations flag set.

Vit Stepanek
23rd November 2010, 19:54
Wow, that's quite easier!
I see this is what I was looking for. Thanks a lot.
Vit