【发布时间】: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() < sceneRect.right())&&(p.y() > sceneRect.top())&& (p.y() < sceneRect.bottom())){ QMessageBox 消息; msg.setText("点是:" + QString::number(p.x()) + ", " + QString::number(p.y())); msg.exec(); }我在哪里测试坐标。但它不会返回正确的结果。 如何限制用户仅在场景中绘制?
【问题讨论】:
标签: qt4 qgraphicsview