如何在 QGraphicsScene 中添加项目?
我正在尝试在鼠标单击和鼠标光标坐标处的 QGraphicsScene 中添加一些自定义 QGraphicsItems。但这些项目不会添加到与鼠标光标相同的坐标处。
renderArea::renderArea(QWidget *parent): QGraphicsView(parent) { scene = new QGraphicsScene(this); scene->setItemIndexMethod(QGraphicsScene::NoIndex); scene->setSceneRect(0, 0, 850, 480); setScene(scene); setCacheMode(CacheBackground); setViewportUpdateMode(BoundingRectViewportUpdate); setRenderHint(QPainter::Antialiasing); setTransformationAnchor(AnchorUnderMouse); scale(qreal(1.0), qreal(1.0)); setMinimumSize(400, 400); } void renderArea::mousePressEvent(QMouseEvent *event) { QPoint p = event->pos(); updateList(p); } void renderArea::updateList(const QPoint &p) { Point point; point.point = p; point.isSelected = false; list.append(point); if (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); scene->addItem(temp); }
renderArea 是 QGraphicsView,Clothoids 是自定义 QGraphicsItem
Clothoid::Clothoid(QPoint startPoint, QPoint endPoint) { sPoint = startPoint; ePoint = endPoint; startCurvature = 0.0; endCurvature = 0.0; clothoidLength = sqrt(pow(endPoint.x() - startPoint.x(),2) + pow(endPoint.y() - startPoint.y(),2)); } QRectF Clothoid::boundingRect() const { qreal penWidth = 1; if ((sPoint.x() < ePoint.x()) && (sPoint.y() < ePoint.y())) return 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())) return 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())) return 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())) return QRectF(ePoint.x(), ePoint.y(), sPoint.x() - ePoint.x(), sPoint.y() - ePoint.y()) .normalized() .adjusted(-penWidth, -penWidth, penWidth, penWidth); return QRectF(); } void Clothoid::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { QLineF line(sPoint, ePoint); // Draw the line itself painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->drawLine(line); }
我猜测我插入项目的坐标属于 GraphicsView,而不是场景,因为在我的应用程序中,场景不覆盖整个视图。但我怎样才能获得我的场景的坐标呢?
I am trying to add some custom QGraphicsItems in a QGraphicsScene on mouse click and at mouse cursor coordinates. But the items are not added at the same coordinates as the mouse cursor's.
renderArea::renderArea(QWidget *parent): QGraphicsView(parent) { scene = new QGraphicsScene(this); scene->setItemIndexMethod(QGraphicsScene::NoIndex); scene->setSceneRect(0, 0, 850, 480); setScene(scene); setCacheMode(CacheBackground); setViewportUpdateMode(BoundingRectViewportUpdate); setRenderHint(QPainter::Antialiasing); setTransformationAnchor(AnchorUnderMouse); scale(qreal(1.0), qreal(1.0)); setMinimumSize(400, 400); } void renderArea::mousePressEvent(QMouseEvent *event) { QPoint p = event->pos(); updateList(p); } void renderArea::updateList(const QPoint &p) { Point point; point.point = p; point.isSelected = false; list.append(point); if (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); scene->addItem(temp); }
renderArea being the QGraphicsView and Clothoids the custom QGraphicsItem
Clothoid::Clothoid(QPoint startPoint, QPoint endPoint) { sPoint = startPoint; ePoint = endPoint; startCurvature = 0.0; endCurvature = 0.0; clothoidLength = sqrt(pow(endPoint.x() - startPoint.x(),2) + pow(endPoint.y() - startPoint.y(),2)); } QRectF Clothoid::boundingRect() const { qreal penWidth = 1; if ((sPoint.x() < ePoint.x()) && (sPoint.y() < ePoint.y())) return 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())) return 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())) return 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())) return QRectF(ePoint.x(), ePoint.y(), sPoint.x() - ePoint.x(), sPoint.y() - ePoint.y()) .normalized() .adjusted(-penWidth, -penWidth, penWidth, penWidth); return QRectF(); } void Clothoid::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { QLineF line(sPoint, ePoint); // Draw the line itself painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->drawLine(line); }
I am guessing that the coordinates to which I am inserting the items belong to the GraphicsView and not the scene as in my application the scene doesn't cover the entire view. But how could I get the coordinates of the scene in my case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的,坐标是相对于 GraphicView 的,而不是
取自 的 场景Qt的文档:
希望它们提供方便的功能(摘自 QGraphicsView 文档):
因此,您要查找的函数是 mapToScene()可以直接调用,因为 renderArea 继承自 QGraphicsView
Edit :注意,
mapToScene()
返回一个QPointF
,而不是QPoint
。本身不是问题,但您应该意识到这一点。You're right, the coordinates are relative to the GraphicView, not the Scene
Taken from the Qt's documentation :
Hopefully, they provide convenience functions (excerpt from the QGraphicsView doc) :
So, the function you're looking for is mapToScene() that you can call directly because renderArea inherits from QGraphicsView
Edit : Watch out,
mapToScene()
returns aQPointF
, not aQPoint
. Not a problem in itself, but you should be aware of this.