In a subclassed QGraphicsTextItem, I'm drawing some text and scaling it down in size to fit into a small area in the Scene. (I found that I have to scale, because the needed size is smaller than what I can get by using a small point size with setFont().) I adjust the scale as follows, depending on the maxHeight that I want to see in the scene.
setFont
(QFont("Times",
40));
setDefaultTextColor
(QColor(Qt
::white));
qreal s = maxHeight / fm.height();
scale(s, -s);
translate(-(fm.width(net)/2), -(fm.height()/2));
setPlainText(net);
setFont(QFont("Times", 40));
setDefaultTextColor(QColor(Qt::white));
QFontMetricsF fm(font());
qreal s = maxHeight / fm.height();
scale(s, -s);
translate(-(fm.width(net)/2), -(fm.height()/2));
setPlainText(net);
To copy to clipboard, switch view to plain text mode
The text is displayed properly. However, when I try zooming to fit, using the code below, the sceneRect is much, much larger than what I expect. So, the zoom doesn't work right. Without the text everything works fine. When I add the text the zoomed view shows everything crammed into a tiny area in a corner with empty space covering the rest of the view. It's almost as if the sceneRect() is being computed using unscaled, rather than scaled text.
pcbDoc
->getScene
()->setSceneRect
(QRectF());
view->fitInView(pcbDoc->getScene()->sceneRect(), Qt::KeepAspectRatio);
pcbDoc->getScene()->setSceneRect(QRectF());
view->fitInView(pcbDoc->getScene()->sceneRect(), Qt::KeepAspectRatio);
To copy to clipboard, switch view to plain text mode
Obviously I'm doing something wrong, but I need a hint.
Thanks!
Bookmarks