gpt4 book ai didi

qt - 如何扩大 QGraphicsItem 的悬停区域

转载 作者:行者123 更新时间:2023-12-04 00:44:26 27 4
gpt4 key购买 nike

我有一个带有相当小的点标记的 QGraphicsScene。我想扩大这些标记的区域以使拖动更容易。标记是一个十字,距离原点 +/- 2 像素。我已经重新实现了

QGraphicsItem::contains(const QPointF & point ) const
{
return QRectF(-10,-10,20,20);
}

void hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
setPen(QPen(Qt::red));
update();
}

但标记只有在被光标直接击中时才会变成红色(甚至有点挑剔)。如何扩大“悬停区域”?

最佳答案

如简短评论中所述:通常这些东西是通过边界矩形或形状函数处理的,尝试重载它们。查看形状下 QGraphicsItem 的 qt 帮助(http://doc.qt.io/qt-4.8/qgraphicsitem.html#shape):

Returns the shape of this item as a QPainterPath in local coordinates. The shape is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions.

The default implementation calls boundingRect() to return a simple rectangular shape, but subclasses can reimplement this function to return a more accurate shape for non-rectangular items. For example, a round item may choose to return an elliptic shape for better collision detection. For example:

QPainterPath RoundItem::shape() const { QPainterPath path; path.addEllipse(boundingRect()); return path; } The outline of a shape can vary depending on the width and style of the pen used when drawing. If you want to include this outline in the item's shape, you can create a shape from the stroke using QPainterPathStroker.

This function is called by the default implementations of contains() and collidesWithPath().

所以基本上发生的是所有想要访问与项目关联的“区域”的函数,调用形状然后执行例如对生成的画家路径进行包含或碰撞检测。因此,如果您有小元素,您应该扩大形状区域。例如,假设一条线是您的目标,那么您的形状实现可能如下所示:

QPainterPath Segment::shape() const{
QLineF temp(qLineF(scaled(Plotable::cScaleFactor)));
QPolygonF poly;
temp.translate(0,pen.widthF()/2.0);
poly.push_back(temp.p1());
poly.push_back(temp.p2());
temp.translate(0,-pen.widthF());
poly.push_back(temp.p2());
poly.push_back(temp.p1());
QPainterPath path;
path.addPolygon(poly);
return path;
}

Pen 是段的成员,我用它的宽度来扩大形状区域。但是您也可以采用与对象的实际尺寸有良好关系的任何其他东西。

关于qt - 如何扩大 QGraphicsItem 的悬停区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13669367/

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