且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在QObject中更改鼠标光标-QGraphicsItem继承的类

更新时间:2023-11-20 20:15:58

您可能可以使用在您的类构造函数中,请确保您...

In your class constructor make sure you do...

setAcceptHoverEvents(true);

然后覆盖 hoverEnterEvent

Then override hoverEnterEvent and hoverLeaveEvent.

virtual void hoverEnterEvent (QGraphicsSceneHoverEvent *event) override
{
  QGraphicsItem::hoverEnterEvent(event);
  QApplication::setOverrideCursor(Qt::PointingHandCursor);
}

virtual void hoverLeaveEvent (QGraphicsSceneHoverEvent *event) override
{
  QGraphicsItem::hoverLeaveEvent(event);
  QApplication::setOverrideCursor(Qt::ArrowCursor);
}

作为旁注:您实际上是否同时继承了QObject QGraphicsItem?如果是这样,您可能只需从 QGraphicsObject .

As a side note: do you actually inherit from both QObject and QGraphicsItem? If so, you could probably achieve the same goal by simply inheriting from QGraphicsObject.

在回答...

我在整个边界区域都有指点图标,我该如何 仅缩小我的绘图区域(在这种情况下为圆形)?

I have the pointing hand icone in my whole bounding rect, how can I reduce the area only to my drawing (in this case a circle) ?

覆盖 QGraphicsItem::shape 以返回表示的QPainterPath 实际形状...

virtual QPainterPath shape () const override
{
  QPainterPath path;

  /*
   * Update path to represent the area in which you want
   * the mouse pointer to change.  This will probably be
   * based on the code in your 'paint' method.
   */
  return(path);
}

现在覆盖 QGraphicsItem::hoverMoveEvent 以使用shape方法...

Now override QGraphicsItem::hoverMoveEvent to make use of the shape method...

virtual void hoverMoveEvent (QGraphicsSceneHoverEvent *event) override
{
  QGraphicsItem::hoverMoveEvent(event);
  if (shape().contains(event->pos())) {
    QApplication::setOverrideCursor(Qt::PointingHandCursor);
  } else {
    QApplication::setOverrideCursor(Qt::ArrowCursor);
  }
}

显然,以上内容可能会对性能产生影响,具体取决于所绘制形状的复杂性,因此取决于QPainterPath.

The above could, obviously, have an impact on performance depending on the complexity of the shape drawn and, hence, the QPainterPath.

(注意:您可以使用QGraphicsItem::shape > QGraphicsItem::opaque .)

(Note: rather than using QGraphicsItem::shape you could do a similar thing with QGraphicsItem::opaque instead.)