Results 1 to 7 of 7

Thread: QPainter::drawText - nothing drawn

  1. #1
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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?

    Qt Code:
    1. void DgChartView::drawBackground( QPainter* painter, const QRectF& rect )
    2. {
    3. painter->drawText( rect.center(), "SOME TEXT SOME TEXT" );
    4.  
    5. for ( QVector<GridData>::iterator it = m_bgGridVert.begin( ); it < m_bgGridVert.end( ); it++ )
    6. {
    7. GridData& data = *it;
    8. QLineF line( QPointF( data.pos, rect.top( ) ), QPointF( data.pos, rect.bottom( ) ) );
    9. painter->drawLine( line );
    10. }
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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.
    Last edited by high_flyer; 12th November 2010 at 16:08.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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 View Post
    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;
    Qt Code:
    1. QString str( "TEXT TEXT" );
    2. painter->save();
    3. QFont font( "Helvetica" );
    4. font.setPointSize( 64 );
    5. painter->setFont( font );
    6. QFontMetrics fMetrics = painter->fontMetrics();
    7. QSize sz = fMetrics.size( Qt::TextSingleLine, str );
    8. QRectF txtRect( rect.center(), sz );
    9. painter->drawText( txtRect, Qt::TextSingleLine, str );
    10. painter->drawRect( txtRect );
    11. ...
    12. painter->restore();
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPainter::drawText - nothing drawn

    Why don't you just a QGraphicsTextItem?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. painter->save();
    2. QPoint viewCoord = mapFromScene( QPoint( data.pos, rect.bottom() ) );
    3. painter->setMatrixEnabled(false);
    4. painter->drawText( viewCoord, data.description );
    5. painter->restore();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPainter::drawText - nothing drawn

    You would have really saved yourself the trouble by using a text item with ItemIgnoresTransformations flag set.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    Vit Stepanek (23rd November 2010)

  8. #7
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter::drawText - nothing drawn

    Wow, that's quite easier!
    I see this is what I was looking for. Thanks a lot.
    Vit

Similar Threads

  1. QPainter::drawText, draw big text
    By franco.amato in forum Newbie
    Replies: 3
    Last Post: 18th March 2010, 09:32
  2. QPainter drawText with different language
    By lni in forum Qt Programming
    Replies: 4
    Last Post: 11th June 2009, 20:54
  3. QPainter::drawText
    By h123 in forum Qt Programming
    Replies: 8
    Last Post: 15th November 2008, 11:10
  4. Qt drawText QPainter
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2008, 20:00
  5. HTML text drawn with QPainter::drawText()
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2008, 02:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.