2015-05-15 54 views
2

我很少關心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由我調整項目。

謝謝:)

+0

要考慮到'boundingRect()'方法返回項的邊界矩形座標,因此它不會受到變換,如規模。 – Tarod

+0

我不知道什麼是更快...在這裏寫這樣的問題,並等待迴應,或寫兩行代碼,並自己測試。 – Greenflow

+0

@Greenflow大概解決問題的最好方法是使用代碼。 – Tarod

回答

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(); 
+0

感謝您解釋塔羅德:) – Zing

+0

不客氣!請儘可能使用'QGraphicsItem :: setScale'。 – Tarod

+0

但這種方法有一個參數。我想改變高度和寬度,比例可以不同 – Zing