2014-10-27 53 views
0

當我嘗試旋轉一個QGraphicsSvgItem對象並設置一個位置時,我遇到了一個奇怪的問題/功能。QGraphicsSvgItem旋轉後定位

檢查以下示例: 爲了簡單起見,我有一個.svg,它是一個藍色的矩形。我創建了兩個使用該文件的QGraphicsSvgItem,並旋轉90度。我將它們都設置在相同的位置,但旋轉的一個位置「錯誤」(也許這是正確的行爲)。它看起來像x,y座標(左上角)現在是右上角,仍然像它沒有旋轉。從我的觀點來看,這看起來很奇怪,我期望當我旋轉並設置位置時,形狀的「第一點」應該是形狀「開始」的位置。

我希望能與源代碼更容易理解我上面寫道:

SVGDom *bubbleDom = shadowgram->bubble(); 
    _bubble = new DrawableSvgItem(sh->message(), bubbleDom->byteArray()); 
    this->addItem(_bubble); 
    _bubble->setPos((this->width()-_bubble->sceneBoundingRect().width())/2, (this->height() - _bubble->sceneBoundingRect().height())/2); 
    qDebug() <<"Bubble pos: " << _bubble->pos(); 
    qDebug() <<"Bubble boundindRect: " << _bubble->boundingRect(); 
    qDebug() << "Bubble rect: " <<_bubble->sceneBoundingRect(); 
    qDebug() << "Bubble topleft: " <<_bubble->sceneBoundingRect().topLeft(); 

    DrawableSvgItem *bubble2 = new DrawableSvgItem(sh->message(), bubbleDom->byteArray()); 
    this->addItem(bubble2); 
    bubble2->setRotation(90); 
    bubble2->setPos(_bubble->pos()); 
    qDebug() << "Bubble2 pos: " << bubble2->pos(); 
    qDebug() <<"Bubble2 boundindRect: " << bubble2->boundingRect(); 
    qDebug() << "Bubble2 rect: " <<bubble2->sceneBoundingRect(); 
    qDebug() <<"Bubble2 topleft: " << bubble2->sceneBoundingRect().topLeft(); 

DrawableSvgItem只是QGraphicsSvgItem將的延伸,我繪製sourcepaths到項目創建。目前我沒有使用它。

輸出:

Bubble pos: QPointF(413.5, 297) 
Bubble boundindRect: QRectF(0,0 171x117) 
Bubble rect: QRectF(296.5,297 117x171) 
Bubble topleft: QPointF(296.5, 297) 
Bubble2 pos: QPointF(413.5, 297) 
Bubble2 boundindRect: QRectF(0,0 171x117) 
Bubble2 rect: QRectF(411.458,297 173.016x119.967) 
Bubble2 topleft: QPointF(411.458, 297) 

在照片,我希望以下內容:

enter image description here

但我得到了下面的圖片:

enter image description here

我發現了一個類似的問題here

回答

1

bubble2圍繞其轉換原點旋轉。這是在bubble2的座標中默認爲0,0。此外bubble2->setPos()將此點設置爲_bubble的0,0,因此您只需將bubble2->setPos()調整爲想要的位置。您可以將變換原點改爲setTransformOriginPoint()

編輯2014年11月5日:bubble2的

左上在bubble2座標定義。這些座標不會因項目轉換而改變,在旋轉之後,項目的左上角與之前相同。這一點是由setPos()放置的。所以圖2顯示了正確的行爲。您想放置在泡泡(x,y)上的角落是bubble2的左下角。爲了讓圖片1,你只需要1行添加到您的代碼: 無論是它的高度移動bubble2在最後一步向右

bubble2->setpos(413.5 + 117, 297) 

或bubble2的第一步組變換原點,以bottem左上角

bubble2->setTransformOriginPoint(0,117) 
+0

感謝您的回答和您的時間。我會嘗試玩這個。 – flatronka 2014-10-28 10:48:27

+0

這並不能真正解決我的問題。我嘗試過,但幾乎發生同樣的事情。 – flatronka 2014-11-04 17:34:29

+0

請參閱我的答案中的編輯 – 2014-11-05 11:31:31