hayzel
15th January 2015, 17:31
Hi,
I design a QGraphicsItem derived class that is like a plot item.
Internallly to draw some text on in I used:
void GIPlot::drawText(QPainter* painter, const QPointF& pos, const QString& text, QFont* font, Qt::Alignment align, QRectF* boundingRect)
{
QFont fnt;
QRectF bRect;
qreal m11,m22;
if(font==0) fnt=QFont(baseFont); else fnt=QFont(*font);
painter->setFont(fnt);
QFontMetricsF fm(fnt);
qreal sw,sh;
sw=fm.width(text);sh=fm.height();
QRectF rect(-sw, -sh,2*sw,2*sh);
bRect=QRectF(0,0,sw,sh);
QPointF p(pos);
if(align & Qt::AlignLeft) p+=QPointF(-sw,0);
else if(align & Qt::AlignRight) p+=QPointF(sw,0);
else if(align & Qt::AlignHCenter) p+=QPointF(-sw/2,0);
if(align & Qt::AlignTop && isYInverted()) p+=QPointF(0,-sh);
else if(align & Qt::AlignBottom && !isYInverted()) p+=QPointF(0,-sh);
else if(align & Qt::AlignVCenter) p+=QPointF(0,-sh/2);
bRect.moveTopLeft(p);
if(boundingRect!=0)//just return the boundingrect
{
*boundingRect=bRect;
return;
};
painter->save();
painter->translate(pos);
m11=painter->matrix().m11();m22=painter->matrix().m22();
painter->setViewTransformEnabled(false);
painter->scale(1.0/m11,1.0/m22);
QRectF rr;
qDebug()<<"fs1:"<<painter->font().pointSizeF();
painter->drawText(rect, align, text,&rr);
fm=QFontMetricsF(painter->fontMetrics());
sw=fm.width(text);sh=fm.height();
qDebug()<<"drawText:"<<text<<rr<<sw<<sh;
painter->restore();
painter->drawRect(bRect);
}
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:
http://wstaw.org/m/2015/01/15/plasma-desktopvCO437.png
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 :
http://wstaw.org/m/2015/01/15/plasma-desktopgcC437.png
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.
I design a QGraphicsItem derived class that is like a plot item.
Internallly to draw some text on in I used:
void GIPlot::drawText(QPainter* painter, const QPointF& pos, const QString& text, QFont* font, Qt::Alignment align, QRectF* boundingRect)
{
QFont fnt;
QRectF bRect;
qreal m11,m22;
if(font==0) fnt=QFont(baseFont); else fnt=QFont(*font);
painter->setFont(fnt);
QFontMetricsF fm(fnt);
qreal sw,sh;
sw=fm.width(text);sh=fm.height();
QRectF rect(-sw, -sh,2*sw,2*sh);
bRect=QRectF(0,0,sw,sh);
QPointF p(pos);
if(align & Qt::AlignLeft) p+=QPointF(-sw,0);
else if(align & Qt::AlignRight) p+=QPointF(sw,0);
else if(align & Qt::AlignHCenter) p+=QPointF(-sw/2,0);
if(align & Qt::AlignTop && isYInverted()) p+=QPointF(0,-sh);
else if(align & Qt::AlignBottom && !isYInverted()) p+=QPointF(0,-sh);
else if(align & Qt::AlignVCenter) p+=QPointF(0,-sh/2);
bRect.moveTopLeft(p);
if(boundingRect!=0)//just return the boundingrect
{
*boundingRect=bRect;
return;
};
painter->save();
painter->translate(pos);
m11=painter->matrix().m11();m22=painter->matrix().m22();
painter->setViewTransformEnabled(false);
painter->scale(1.0/m11,1.0/m22);
QRectF rr;
qDebug()<<"fs1:"<<painter->font().pointSizeF();
painter->drawText(rect, align, text,&rr);
fm=QFontMetricsF(painter->fontMetrics());
sw=fm.width(text);sh=fm.height();
qDebug()<<"drawText:"<<text<<rr<<sw<<sh;
painter->restore();
painter->drawRect(bRect);
}
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:
http://wstaw.org/m/2015/01/15/plasma-desktopvCO437.png
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 :
http://wstaw.org/m/2015/01/15/plasma-desktopgcC437.png
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.