首页| 论坛| 消息

标题:某些相同的位置,view能获得图元,sence不能获得图元
作者:hy1hy1
日期:2020-03-30 11:23
内容:

调用Sence的setSceneRect函数设置大小为(0,0,600,400);继承QGraphicsItem,实现绘制直线(0,0)到(200,200);为了精确选择直线,重载了shape函数,
但当鼠标点击时,QGraphicsView调用item()函数总是能返回图元,但使用QGraphicsSence调用ItemAt()函数确返回为空指针(某些相同的位置,view能获得图元,sence不能获得图元)。
QRectF CLineItem::boundingRect() const
{
return shape().controlPointRect();
}
QPainterPath CLineItem::shape() const
{
QPainterPath path;
if (m_line == QLineF())
return path;
path.moveTo(m_line.p1());
path.lineTo(m_line.p2());
QPainterPathStroker stroker;
stroker.setWidth(2);
stroker.setJoinStyle(Qt::MiterJoin);
stroker.setCapStyle(Qt::RoundCap);
stroker.setDashPattern(Qt::DashLine);
return stroker.createStroke(path);
return path;
}
void XXXGraphicsView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
QPoint point= event->pos();
for(int i=0; i


#1 [maxlogo 03-30 20:20]
应该是写错了,鼠标位置应该用event->pos()获取,scene的itemAt应该使用的是view的viewportTransform()函数
#2 回 maxlogo 的帖子 [hy1hy1 03-31 09:35]
maxlogo:应该是写错了,鼠标位置应该用event->pos()获取,scene的itemAt应该使用的是view的viewportTransform()函数 (2020-03-30 20:20) 
非常感谢。因为整个区域大小为600*400,只绘制了一条0,0到200,200的直线;通过鼠标点击时打印出所有位置的图元(并不是获取鼠标位置的图元)。另外,调用过viewportTransform()函数,结果没有什么区别。view的itemAt函数能识别的点个数比sence的itemAt函数能识别的点个数要多一些。
#3 [maxlogo 03-31 10:03]
if ((d->identityMatrix || d->matrix.type() scene->items(xinv.mapRect(QRectF(pos.x(), pos.y(), 1, 1)),
Qt::IntersectsItemShape,
Qt::DescendingOrder,
viewportTransform());
}
// Use the polygon version
return d->scene->items(mapToScene(pos.x(), pos.y(), 1, 1),
Qt::IntersectsItemShape,
Qt::DescendingOrder,
viewportTransform());
看源码就知道了,view的itemAt使用的这个

回复 发表
主题 版块