c++ - 使用 Qt 和 C++ 进行转换

标签 c++ qt

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

initial position

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

position after mouse click

得到交点(直线和圆之间)的坐标后,我想将圆弧中心的位置设置为交点。 但是,如您所见,弧线并不在我希望的位置。奇怪的是,当我绘制一个 topLeft 点位于交点的矩形时,它起作用了:

enter image description here

我想问题一定出在场景/父/项目坐标上……但我找不到位置:/

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

void DrawingScene::drawStates(){
    m_ellipse = new QGraphicsEllipseItem(80.0, 80.0,120.0,120.0);
    addItem(m_ellipse);
    m_arc = new GraphicArcItem(62.0, 62.0,50.0,50.0);
    m_arc->setParentItem(m_ellipse);
    m_arc->setStartAngle(0);
    m_arc->setSpanAngle(270 * 16);
    QLineF line_vertical(140.0,80.0,140.0,200.0);
    addLine(line_vertical);
    QLineF line_horizontal(80.0,140.0,200.0,140.0);
    addLine(line_horizontal);
    QLineF line(QPointF(62.0,62.0), QPointF(140.0,140.0));
    m_lineToCenter = new QGraphicsLineItem(line);
    addItem(m_lineToCenter);
}

void DrawingScene::mousePressEvent(QGraphicsSceneMouseEvent *event){
    p1 = event->scenePos();
    QLineF lineToCenter(QPointF(140.0,140.0), p1);
    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 intersectionPoint(x,y);
    QPointF topLeft = intersectionPoint + QPointF(140.0,140.0);
    addRect(QRectF(topLeft, QSizeF(60.0,60.0)));
    m_arc->setPos(topLeft);
}

我们非常欢迎任何帮助:)

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

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_ellipse,因此它们坐标的引用不同。要测试它只需添加一个新的圆弧/圆(不同于m_arc 并且不使用 setParentItem(m_ellipse); ) 到场景 - 它应该具有正确的屏幕位置。为了达到预期的结果,我建议您使用 mapTo<something>/mapFrom<something> 方法。我认为 mapToParent 将做这个把戏,但无论如何你都应该检查一下。

关于c++ - 使用 Qt 和 C++ 进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50365606/

相关文章:

c++ - 创建集合 vector

c++ - std::align 的使用问题

c++ - std::map 或类似数据结构中的键压缩

c++ - 在 QT 中创建成功的 SSL 握手的正确方法是什么?

c++ - QGLWidget上下文破坏

c++ - Qt QScrollArea 自动滚动

C++ EVP_EncriptUpdate 重写堆栈?

c++ - 为什么我们不能将 "="用于 shared_ptr 或 unique_ptr?

c++ - Qtopengl,为什么不能使用不同的 vbo 绘制两个立方体

c++ - QTableView 中的 Qt 复选框