PDA

View Full Version : Scaling QGraphicsRectItem child



mihnen
12th September 2012, 15:57
I'm trying to figure out how to scale a child item of QGraphicsRectItem inside of the rectangle. Basically something like fitInView() except I want it to fit in the QGraphicsRectItem. Any ideas? Here is my currently non working code where m_textItem is a subclassed QGraphicsTextItem, this is called on the m_textItem->document() contentsChange(int,int,int) event.



void TextItemContainer::fitItemInRect()
{
if(!m_textItem) {
qDebug() << "fitItemInRet() call failed because of condition: if(!m_textItem)";
return;
}

qDebug() << "Inside fitItemInRect()";
qDebug() << "resetting scaling....";

// Reset the scaling
m_textItem->scale(1, 1);

qreal xratio = this->boundingRect().width() / m_textItem->boundingRect().width();
qreal yratio = this->boundingRect().height() / m_textItem->boundingRect().height();

qDebug() << "xratio ....: " << xratio;
qDebug() << "yratio ....: " << yratio;

// Keep aspect ratio
xratio = yratio = qMin(xratio, yratio);

m_textItem->scale(xratio, yratio);
}

wysota
12th September 2012, 16:15
scale(1,1) does not reset the scaling. It is a no-op. setScale(1) would reset the scale.

mihnen
12th September 2012, 16:32
ahh, gotcha. I actually did get it working with:


m_textItem->setTransform(QTransform::fromScale(1, 1));

is there any difference between doing it this way or using setScale

wysota
12th September 2012, 21:57
It's equivalent of setScale(1).