2013-12-23 36 views
2

我想繪製一個角度的矩形。它可以工作,但是當我改變角度時,矩形的位置正在改變。我無法理解它。有人給我一隻手嗎?QPainterPath QTransform :: map

這裏是我的代碼:

QPoint point = QPoint(100,100); // has to be shown at this point 
QSize size = QSize(30,30); 
QRect rect = QRect(point,size); 


QPainterPath Path ; 
Path.addRect(rect); 

QTransform t; 
t.rotate(myAngle); 

QPainterPath newPath= t.map(Path); 

QwtPlotShapeItem *Item = new QwtPlotShapeItem("Shape Name"); 
Item->setItemAttribute(QwtPlotItem::Legend, true); 
Item->setRenderHint(QwtPlotItem::RenderAntialiased, true); 
Item->setShape(newPath); 
Item->setPen(Qt::black); 
Item->setBrush(QColor("Grey")); 
Item->attach(this); 

我認爲map()函數導致此問題。但我不知道爲什麼。感謝您的建議

回答

1

QTransform::rotate使用(0,0)中心點旋轉座標系。你的矩形不在中心,所以在旋轉的時候它會顯着移動。您應該將矩形放置在座標系的中心(點=( - 15,-15)),並在t.rotate之後使用t.translate將旋轉的矩形移動到適當的位置。