QTransform scale和boundingRect.size()之间的关系

时间:2015-05-15 08:55:25

标签: c++ qt scale qgraphicsitem

我很少关心QTransform规模与QGraphicsItem的boundingRect()方法中的宽度和高度返回值之间的关系。 实际上我想将QGraphicsItem缩放为其boundingRect大小。即,如果我的项目的大小最初为100,100,我在boundingRect()方法中传递,之后我通过mousemove事件增加item的大小。如果我增加的宽度和高度分别是400,300,那我的比例系数是4,3? 任何帮助都会很明显。

这是代码

this->setPos(minMax().first.x(), minMax().first.y());


qreal w = minMax().second.x() - minMax().first.x();
qreal h = minMax().second.y() - minMax().first.y();

qreal scaleFactorW = w / boundingRect().width();
qreal scaleFactorH = h / boundingRect().height();

QTransform trans;
trans.scale(scaleFactorW, scaleFactorH);
setTransform(trans);

bottomPoints = QPointF(w, h);

minMax功能是:

float xMin = 0, xMax = 0, yMin = 0, yMax = 0;
QList<double> xValues, yValues;
xValues << shaper[0]->scenePos().x() << shaper[1]->scenePos().x() << shaper[2]->scenePos().x() << shaper[3]->scenePos().x() << shaper[4]->scenePos().x() << shaper[5]->scenePos().x() << shaper[6]->scenePos().x() << shaper[7]->scenePos().x();
yValues << shaper[0]->scenePos().y() << shaper[1]->scenePos().y() << shaper[2]->scenePos().y() << shaper[3]->scenePos().y() << shaper[4]->scenePos().y() << shaper[5]->scenePos().y() << shaper[6]->scenePos().y() << shaper[7]->scenePos().y();
qSort(xValues.begin(), xValues.end());
qSort(yValues.begin(), yValues.end());
xMin = xValues.first();
xMax = xValues.last();
yMin = yValues.first();
yMax = yValues.last();
return qMakePair(QPointF(xMin, yMin), QPointF(xMax, yMax));

shaper是qgraphicsitem,我正在通过它重新调整项目。

谢谢:)

1 个答案:

答案 0 :(得分:1)

感谢您展示您的代码。

正如我之前所说,

trans.scale(scaleFactorW, scaleFactorH);

不会更改QGraphicsItem::boundingRect返回的尺寸。

但事实上,QGraphicsItem::setScale具有相同的行为,而该项目的boundingRect()也不会改变。

QTransform :: scale和QGraphicsItem :: setScale不相同,但两者都可用于更改图像大小。好吧,在QTransform的情况下,你正在缩放坐标系。

我认为一个例子是解释自己的最佳方式。

this继承QGraphicsItem

qWarning() << "QGraphicsItem::scale(): " << this->scale();
QRectF br = this->boundingRect();
qWarning() << "QGraphicsItem::boundingRect() size / x / y / w / h: " << br.size() << " / "
                                                        << br.x() << " / "
                                                        << br.y() << " / "
                                                        << br.width() << " / "
                                                        << br.height();

QTransform trans = this->transform();
trans.scale(2.0, 2.0);
this->setTransform(trans);

/*
     Comment trans.scale(2.0, 2.0) and uncomment the following line
                to check the difference using the logs.
*/

// this->setScale(2.0);

qWarning() << "QGraphicsItem::scale(): " << this->scale();
br = this->boundingRect();
qWarning() << "QGraphicsItem::boundingRect() size / x / y / w / h: " << br.size() << " / "
                                                        << br.x() << " / "
                                                        << br.y() << " / "
                                                        << br.width() << " / "
                                                        << br.height();

qWarning() << "boundingRect * item_scale: " << this->boundingRect().size() * this->scale();