我正在尝试在鼠标单击和鼠标光标坐标处在 QGraphicsScene 中添加一些自定义 QGraphicsItems。但是这些项目不会添加到与鼠标光标相同的坐标处。
renderArea::renderArea(QWidget *parent): QGraphicsView(父) { 场景 = 新 QGraphicsScene(this); 场景->setItemIndexMethod(QGraphicsScene::NoIndex); 场景->setSceneRect(0, 0, 850, 480); 设置场景(场景); setCacheMode(CacheBackground); setViewportUpdateMode(BoundingRectViewportUpdate); setRenderHint(QPainter::抗锯齿); setTransformationAnchor(AnchorUnderMouse); 规模(qreal(1.0),qreal(1.0)); setMinimumSize(400, 400); } 无效渲染区域::mousePressEvent(QMouseEvent *事件) { QPoint p = event->pos(); 更新列表(p); } void renderArea::updateList(const QPoint &p) { 点点; 点.点 = p; 点.isSelected = false; list.append(point); 如果 (list.size() > 1) updateClothoid(list[list.size()-2].point, list[list.size()-1].point); } void renderArea::updateClothoid(const QPoint &p1, const QPoint &p2) { Clothoid *temp = new Clothoid(p1, p2); clothoids.append(temp); 场景->添加项目(临时); }
renderArea 是 QGraphicsView 和 Clothoids 自定义 QGraphicsItem
Clothoid::Clothoid(QPoint startPoint, QPoint endPoint) { sPoint = 起点; ePoint = 端点; 起始曲率 = 0.0; 结束曲率 = 0.0; ClooidLength = sqrt(pow(endPoint.x() - startPoint.x(),2) + pow(endPoint.y() - startPoint.y(),2)); } QRectF Clothoid::boundingRect() 常量 { qreal penWidth = 1; 如果 ((sPoint.x() < ePoint.x()) && (sPoint.y() < ePoint.y())) 返回 QRectF(sPoint.x(), sPoint.y(), ePoint.x() - sPoint.x(), ePoint.y()-sPoint.y()) .normalized() .adjusted(-penWidth, -penWidth, penWidth, penWidth); if ((sPoint.x() < ePoint.x()) && (sPoint.y() > ePoint.y())) 返回 QRectF(sPoint.x(), ePoint.y(), ePoint.x() - sPoint.x(), sPoint.y() - ePoint.y()) .normalized() .adjusted(-penWidth, -penWidth, penWidth, penWidth); if ((sPoint.x() > ePoint.x()) && (sPoint.y() < ePoint.y())) 返回 QRectF(ePoint.x(), sPoint.y(), sPoint.x() - ePoint.x(), ePoint.y()-sPoint.y()) .normalized() .adjusted(-penWidth, -penWidth, penWidth, penWidth); if ((sPoint.x() > ePoint.x()) && (sPoint.y() > ePoint.y())) 返回 QRectF(ePoint.x(), ePoint.y(), sPoint.x() - ePoint.x(), sPoint.y() - ePoint.y()) .normalized() .adjusted(-penWidth, -penWidth, penWidth, penWidth); 返回 QRectF(); } void Clothoid::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { QLineF 线(sPoint, ePoint); // 自己画线 画家->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); 画家->画线(线); }
我猜我插入项目的坐标属于 GraphicsView 而不是场景,因为在我的应用程序中,场景并没有覆盖整个视图。但是在我的情况下,我怎么能得到场景的坐标呢?