Results 1 to 2 of 2

Thread: qpainter drawtext boundingRect problem

  1. #1
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qpainter drawtext boundingRect problem

    Hi,
    I design a QGraphicsItem derived class that is like a plot item.
    Internallly to draw some text on in I used:
    Qt Code:
    1. void GIPlot::drawText(QPainter* painter, const QPointF& pos, const QString& text, QFont* font, Qt::Alignment align, QRectF* boundingRect)
    2. {
    3. QFont fnt;
    4. QRectF bRect;
    5. qreal m11,m22;
    6. if(font==0) fnt=QFont(baseFont); else fnt=QFont(*font);
    7. painter->setFont(fnt);
    8. QFontMetricsF fm(fnt);
    9. qreal sw,sh;
    10. sw=fm.width(text);sh=fm.height();
    11. QRectF rect(-sw, -sh,2*sw,2*sh);
    12. bRect=QRectF(0,0,sw,sh);
    13. QPointF p(pos);
    14. if(align & Qt::AlignLeft) p+=QPointF(-sw,0);
    15. else if(align & Qt::AlignRight) p+=QPointF(sw,0);
    16. else if(align & Qt::AlignHCenter) p+=QPointF(-sw/2,0);
    17. if(align & Qt::AlignTop && isYInverted()) p+=QPointF(0,-sh);
    18. else if(align & Qt::AlignBottom && !isYInverted()) p+=QPointF(0,-sh);
    19. else if(align & Qt::AlignVCenter) p+=QPointF(0,-sh/2);
    20. bRect.moveTopLeft(p);
    21. if(boundingRect!=0)//just return the boundingrect
    22. {
    23. *boundingRect=bRect;
    24. return;
    25. };
    26. painter->save();
    27. painter->translate(pos);
    28. m11=painter->matrix().m11();m22=painter->matrix().m22();
    29. painter->setViewTransformEnabled(false);
    30. painter->scale(1.0/m11,1.0/m22);
    31. QRectF rr;
    32. qDebug()<<"fs1:"<<painter->font().pointSizeF();
    33. painter->drawText(rect, align, text,&rr);
    34. fm=QFontMetricsF(painter->fontMetrics());
    35. sw=fm.width(text);sh=fm.height();
    36. qDebug()<<"drawText:"<<text<<rr<<sw<<sh;
    37. painter->restore();
    38. painter->drawRect(bRect);
    39. }
    To copy to clipboard, switch view to plain text mode 

    I added some qDebug() messages and drawRect around text bRect for debugging reasons.
    I make a scene, set a view and fit my object to view (fitInView). And I get this:



    As you can see all is rendered fine. rr rect agrees with the size returned from QFontMetricsF

    Now when I scale the view so my object is a little smaller than the viewport
    I get :



    As you can see the returning bRect (boundingRect) is smaller than the text and also agrees with rr rect (which is also wrong). The difference is clear in the big labels like "fcy=13.33MPa".

    So the question is what is my error in my program logic?
    The drawing function is the same only the scale in the view is changed.

    Some more details:
    I noticed the pointSizeF of my font is always 11.0 even after the qpainter transform.

  2. #2
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qpainter drawtext boundingRect problem

    Problem solved.
    It was wrong that I scaled the painter to (1/m11,1/m22)
    Using :

    Qt Code:
    1. painter->save();
    2. painter->translate(pos);
    3. m11=painter->matrix().m11();m22=painter->matrix().m22();
    4. painter->scale(1.0,-yAxisSign()*1.0);
    To copy to clipboard, switch view to plain text mode 
    the problem is gone.
    Though I have the question , why the painter->font() didn't change its fontMetrics() and why painter->drawText() was returning the wrong boundingRect when I DID change the scale.

Similar Threads

  1. about QPainter::drawText()
    By Nicho in forum Newbie
    Replies: 2
    Last Post: 30th November 2013, 01:21
  2. Replies: 2
    Last Post: 20th September 2009, 03:59
  3. QPainter::drawText
    By h123 in forum Qt Programming
    Replies: 8
    Last Post: 15th November 2008, 11:10
  4. qpainter "drawText() problem
    By impeteperry in forum Qt Programming
    Replies: 10
    Last Post: 25th March 2007, 01:46
  5. Replies: 3
    Last Post: 30th April 2006, 20:22

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.