【问题标题】:How to draw in the scene of the QGraphicsView?QGraphicsView的场景怎么画?
【发布时间】:2011-10-17 18:00:15
【问题描述】:

我有一个应用程序,用户可以在其中用鼠标绘制一些点,我使用 QGraphicsView 来做到这一点。

我在 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);

这个场景没有覆盖整个 QGraphicsView,我希望用户能够只在场景上绘制点。点的坐标也应该是来自场景而不是 QGraphicsView 区域的坐标。

这是一个screenshot!它的外观。

我试过这样做:

QPoint p = event->pos(); QRectF sceneRect = this->sceneRect(); if ((p.x() > sceneRect.left())&&(p.x() &lt sceneRect.right())&&(p.y() > sceneRect.top())&& (p.y() &lt sceneRect.bottom())){ QMessageBox 消息; msg.setText("点是:" + QString::number(p.x()) + ", " + QString::number(p.y())); msg.exec(); }

我在哪里测试坐标。但它不会返回正确的结果。 如何限制用户仅在场景中绘制?

【问题讨论】:

    标签: qt4 qgraphicsview


    【解决方案1】:

    知道了。创建的点的坐标来自 QGraphicsView 而不是场景,因此当将点上的约束置于场景内时,它将不起作用。

    必须将点映射到场景:

    QPointF p = mapToScene(event->pos());

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 2012-01-08
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多