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.