我很少關心QTransform尺度與QGraphicsItem的boundingRect()方法中的寬度和高度返回值之間的關係。 其實我想縮放QGraphicsItem作爲其boundingRect大小。即如果我的項目的大小最初是100,100,那麼我在通過boundingRect()方法傳遞後,我將通過mousemove事件增加項目的大小。如果我的寬度和高度分別增加400,300是我的比例因子4,3? 任何幫助將是可觀的。QTransform尺度與boundingRect.size()之間的關係
這是代碼
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);
最小最大功能是:
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));
成形器的QGraphicsItem由我調整項目。
謝謝:)
要考慮到'boundingRect()'方法返回項的邊界矩形座標,因此它不會受到變換,如規模。 – Tarod
我不知道什麼是更快...在這裏寫這樣的問題,並等待迴應,或寫兩行代碼,並自己測試。 – Greenflow
@Greenflow大概解決問題的最好方法是使用代碼。 – Tarod