QPainter::drawText - nothing drawn
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?
Code:
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;
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
Re: QPainter::drawText - nothing drawn
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.
1 Attachment(s)
Re: QPainter::drawText - nothing drawn
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?
Quote:
Originally Posted by
high_flyer
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;
Code:
painter->save();
QFont font
( "Helvetica" );
font.setPointSize( 64 );
painter->setFont( font );
QSize sz
= fMetrics.
size( Qt
::TextSingleLine, str
);
QRectF txtRect
( rect.
center(), sz
);
painter->drawText( txtRect, Qt::TextSingleLine, str );
painter->drawRect( txtRect );
...
painter->restore();
Re: QPainter::drawText - nothing drawn
Why don't you just a QGraphicsTextItem?
Re: QPainter::drawText - nothing drawn
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.
Code:
painter->save();
QPoint viewCoord
= mapFromScene
( QPoint( data.
pos, rect.
bottom() ) );
painter->setMatrixEnabled(false);
painter->drawText( viewCoord, data.description );
painter->restore();
Re: QPainter::drawText - nothing drawn
You would have really saved yourself the trouble by using a text item with ItemIgnoresTransformations flag set.
Re: QPainter::drawText - nothing drawn
Wow, that's quite easier!
I see this is what I was looking for. Thanks a lot.
Vit