我想画一个有角的矩形。它有效,但是当我改变角度时,矩形的位置在其他地方发生变化。我无法理解。有人帮我吗?
这是我的代码:
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()函数导致了这个问题。但我不知道为什么。谢谢各位的建议
最佳答案
QTransform::rotate
使用 (0, 0) 中心点旋转坐标系。您的矩形不在中心,因此在旋转时它将显着移动。您应该将矩形放置在坐标系的中心(point=(-15, -15)),并在 t.rotate
之后使用 t.translate
将旋转的矩形移动到适当的位置。
关于c++ - QPainterPath QTransform::map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20740086/