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);
}
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);
}