Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用Qt和C+的转换+;_C++_Qt - Fatal编程技术网

C++ 使用Qt和C+的转换+;

C++ 使用Qt和C+的转换+;,c++,qt,C++,Qt,我正在做一个程序来绘制一个圆弧,并根据鼠标光标的位置移动/旋转它: 此图显示了程序首次运行时的场景。 下图说明了鼠标单击后显示的内容。鼠标单击发生在直线的右上角 在获得交点(直线和圆之间)的坐标后,我想将圆弧中心的位置设置为交点。 但是,正如你所看到的,弧线并不是我所希望的那样。奇怪的是,当我画一个左上角点位于交点的矩形时,它可以工作: 我想问题应该出在场景/父对象/项目坐标上。。。但我找不到:/ 下面是代码示例:(DrawingScene继承QGraphicsScene) 欢迎提供任何帮

我正在做一个程序来绘制一个圆弧,并根据鼠标光标的位置移动/旋转它:

此图显示了程序首次运行时的场景。 下图说明了鼠标单击后显示的内容。鼠标单击发生在直线的右上角

在获得交点(直线和圆之间)的坐标后,我想将圆弧中心的位置设置为交点。 但是,正如你所看到的,弧线并不是我所希望的那样。奇怪的是,当我画一个左上角点位于交点的矩形时,它可以工作:

我想问题应该出在场景/父对象/项目坐标上。。。但我找不到:/

下面是代码示例:(DrawingScene继承QGraphicsScene)

欢迎提供任何帮助:)

编辑: 移动圆弧的工作代码:

p1 = event->scenePos();
QLineF lineToCenter(QPointF(140.0,140.0), p1);//center of circle to mouse position
double angleBetweenPositions = lineToCenter.angleTo(m_lineToCenter->line()); 
m_lineToCenter->setLine(lineToCenter);
QLineF horizontalLine(QPointF(140.0,140.0),QPointF(200.0,140.0));
double angleBetweenLines = horizontalLine.angleTo(lineToCenter);

double x = 60.0 * cos(angleBetweenLines * 3.14 / 180.0);
double y = -60.0 * sin(angleBetweenLines * 3.14 / 180.0);
QPointF newPoint(x,y);
QPointF ellipse_center = m_ellipse->rect().center();
QPointF intersection_point = intersection_point + ellipse_center;
GraphicArcItem *arc2 = new GraphicArcItem(intersection_point.rx()- 25.0,
                                          intersection_point.ry() - 25.0,50.0,50.0);
addItem(arc2);
m_arc->setPos(intersection_point.rx()-85.0, intersection_point.ry() - 85.0);//why 85 ??
轮换代码:

m_arc->setCurrentRotation(m_arc->getCurrentRotation() + angleBetweenPositions);
m_arc->setTransformOriginPoint(m_arc->getCenter());
m_arc->setRotation(m_arc->getCurrentRotation());
编辑:以下是解决问题代码的关键部分:

/*Return the center point of the arc in the parent coordinates*/
QPointF GraphicArcItem::getCenter(){
int xCenter = rect().x() + rect().width()/2;
int yCenter = rect().y() + rect().height()/2;
QPointF center = /*mapToParent(*/QPointF(xCenter,yCenter)/*)*/;
return center;
}

p1 = event->scenePos();
QPointF ellipse_center = m_ellipse->rect().center();
QLineF lineToCenter(ellipse_center, p1);//center of circle to mouse position
double angleBetweenPositions = lineToCenter.angleTo(m_lineToCenter->line());
QLineF horizontalLine(ellipse_center,QPointF(200.0,140.0));
double angleBetweenLines = horizontalLine.angleTo(lineToCenter);
double x = 60.0 * cos(angleBetweenLines * 3.14 / 180.0);
double y = -60.0 * sin(angleBetweenLines * 3.14 / 180.0);
QPointF newPoint(x,y);
QPointF intersection_point = newPoint + ellipse_center;
m_arc->setPos(intersection_point.rx() - 85.0, intersection_point.ry() - 85.0);
m_arc->setCurrentRotation(angleBetweenPositions);
QPointF rotation_center = m_arc->mapFromItem(m_arc, m_arc->getCenter());
m_arc->setTransformOriginPoint(rotation_center);
m_arc->setRotation(m_arc->getCurrentRotation());

矩形和圆弧有不同的父对象(场景是矩形和圆弧的
m_椭圆
,因此它们的坐标参考不同。要测试它,只需添加一个新的圆弧/圆(不同于
m_圆弧
,并且不使用
setParentItem(m_椭圆);
)到场景-它应该有正确的屏幕位置。为了达到预期的效果,我建议您使用
mapTo
/
mapFrom
方法。我认为这会起作用,但您还是应该检查它。

如果我删除行
m_弧->setParentItem(m_椭圆)
,那么
m_弧的父级将是场景,对吗?正确。仅此一点就可以解决问题,但我认为它会影响程序的其余逻辑。这就是为什么在这种情况下使用映射。它不会改变程序逻辑,我曾试图解决我的问题,但删除它并不能解决问题问题…:/我的错,有一个改进…现在,它画在圆圈的另一个点上…让我想想,我会让你知道的:)非常感谢你的帮助和你的时间!:)我还有其他事情要做,但我终于有时间研究这个话题,我成功了!
/*Return the center point of the arc in the parent coordinates*/
QPointF GraphicArcItem::getCenter(){
int xCenter = rect().x() + rect().width()/2;
int yCenter = rect().y() + rect().height()/2;
QPointF center = /*mapToParent(*/QPointF(xCenter,yCenter)/*)*/;
return center;
}

p1 = event->scenePos();
QPointF ellipse_center = m_ellipse->rect().center();
QLineF lineToCenter(ellipse_center, p1);//center of circle to mouse position
double angleBetweenPositions = lineToCenter.angleTo(m_lineToCenter->line());
QLineF horizontalLine(ellipse_center,QPointF(200.0,140.0));
double angleBetweenLines = horizontalLine.angleTo(lineToCenter);
double x = 60.0 * cos(angleBetweenLines * 3.14 / 180.0);
double y = -60.0 * sin(angleBetweenLines * 3.14 / 180.0);
QPointF newPoint(x,y);
QPointF intersection_point = newPoint + ellipse_center;
m_arc->setPos(intersection_point.rx() - 85.0, intersection_point.ry() - 85.0);
m_arc->setCurrentRotation(angleBetweenPositions);
QPointF rotation_center = m_arc->mapFromItem(m_arc, m_arc->getCenter());
m_arc->setTransformOriginPoint(rotation_center);
m_arc->setRotation(m_arc->getCurrentRotation());