• 2137阅读
  • 3回复

[提问]某些相同的位置,view能获得图元,sence不能获得图元 [复制链接]

上一主题 下一主题
离线hy1hy1
 

只看楼主 倒序阅读 楼主  发表于: 2020-03-30
关键词: 求助
调用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<601; ++i)
        {
            for(int j=0; j<401; ++j)
            {
                QGraphicsItem* item1 = itemAt(QPoint(i,j));
                if(item1)
                {
                    qDebug()<<"view"<<QPoint(i,j)<<item1; /// 1.打印在该位置view能获取的图元
                }

                QPointF pt = mapToScene(QPoint(i,j));
                QGraphicsItem* item2 = m_pScene->itemAt(pt,transform());
                if(item2)
                {
                    qDebug()<<"sence"<<QPoint(i,j)<<item2;  /// 2.打印在该位置sence能获取的图元(比1少)
                }
            }
        }
    }
    QGraphicsView::mousePressEvent(event);
}
离线maxlogo

只看该作者 1楼 发表于: 2020-03-30
应该是写错了,鼠标位置应该用event->pos()获取,scene的itemAt应该使用的是view的viewportTransform()函数
个人博客:
简书:https://www.jianshu.com/u/14fa805306bd
CSDN:https://blog.csdn.net/qq10097355
思否:https://segmentfault.com/u/lowbees/articles
离线hy1hy1

只看该作者 2楼 发表于: 2020-03-31
回 maxlogo 的帖子
maxlogo:应该是写错了,鼠标位置应该用event->pos()获取,scene的itemAt应该使用的是view的viewportTransform()函数 (2020-03-30 20:20) 

非常感谢。因为整个区域大小为600*400,只绘制了一条0,0到200,200的直线;通过鼠标点击时打印出所有位置的图元(并不是获取鼠标位置的图元)。另外,调用过viewportTransform()函数,结果没有什么区别。view的itemAt函数能识别的点个数比sence的itemAt函数能识别的点个数要多一些。
离线maxlogo

只看该作者 3楼 发表于: 2020-03-31


    if ((d->identityMatrix || d->matrix.type() <= QTransform::TxScale)) {
        // Use the rect version
        QTransform xinv = viewportTransform().inverted();
        return d->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使用的这个
个人博客:
简书:https://www.jianshu.com/u/14fa805306bd
CSDN:https://blog.csdn.net/qq10097355
思否:https://segmentfault.com/u/lowbees/articles
快速回复
限100 字节
 
上一个 下一个