gpt4 book ai didi

c++ - 使用 QGraphicsView 选择很多多段线效果不佳

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:02:05 27 4
gpt4 key购买 nike

在 Qt 中,没有“QGraphicsPolylineItem”,所以我必须从 QGraphicsItem 中实现它。我重新实现了“paint”和“boundingRect”函数。在“paint()”函数中,我只是简单地绘制折线中的所有线。没有任何用户的交互就可以了。对于选择和可移动功能,我重新实现了“QPainterPath* shape()”函数,如下所示:

QPainterPath ContourLineShape::shape() const
{
QPainterPath path;
QPointF p0=this->poly.at(0)->at(0);
path.moveTo(p0.x(),p0.y());
foreach(QPointF p,poly)
path.lineTo(p.x(),p.y());
return path;
}

但是结果是错误的。当我点击一条多段线时,它总是选择另一条。然后我尝试自己实现选择,如下所示:

void GraphicsView::mousePressEvent ( QMouseEvent * event )
{
int x=event->pos().x();
int y=event->pos().y();

QList<QGraphicsItem *>items=this->items(x-5,y-5,10,10,Qt::IntersectsItemShape);
for(int i=items.size()-1;i>=0;i--)
{
QGraphicsItem *item=items.at(i);

if(item->type()==QGraphicsItem::UserType+4)//UserType+4 is my polyline type.
{
item->setSelected(true);
return; //one shape has been selected.
}
}
}

这个解决方案看起来是正确的,但并不准确。如果像这样的折线:

      ----------------------
|
| o<-click here |
| |
| /\ /\ |
| / \ / \ /-----------
/ V V

点击点远离边界线,但形状仍然可以选择(这不是我想要的)。即使我将选择模式更改为 Qt::ContainsItemBoundingRect 或 Qt::ContainsItemShape..,结果仍然是错误的。有什么简单的方法可以解决这个问题吗?或者,我必须计算从“点击点”到所有线段的距离来决定它是否被选中吗?

谢谢!

最佳答案

调用 setFlags(QGraphicsItem::ItemClipsToShape) 有帮助吗?

关于c++ - 使用 QGraphicsView 选择很多多段线效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3612398/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com