QGraphicsRectItem 的信号 itemChange 不在拖动时生成

QGraphicsRectItem's signal itemChange not generating on drag

我从 QGraphicsRectItem 派生了一个自定义 class。

class CornerPoint : public QGraphicsRectItem
{
  public:
  CornerPoint(QPointF centre);

 enum { Type = UserType + 1 };

 int type() const{ return Type; }

QVariant itemChange(GraphicsItemChange change, const QVariant &value);

public slots:

void updatePosition(QPointF);

};

CPP 文件

 CornerPoint::CornerPoint(QPointF centre): QGraphicsRectItem(NULL)
 {
    setPos(55,22);
    setRect(0,0,99,99);
 }

  QVariant CornerPoint::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
    std::cout << "Change is" << change << std::endl;

     if( QGraphicsItem::ItemPositionChange == change && scene())
     {
        std::cout << "New values " << value.toPointF().x() << value.toPointF().y() << std::endl;
     }

    return QGraphicsItem::itemChange(change, value);
 }

我已经让这些对象可以选择和移动。但是当我拖动它们时,我没有收到这个矩形位置变化的信号。我收到的只是值为 4 (ItemSelectedChange) 和 14 (ItemSelectedHasChanged) 的信号。

This post 说我需要启用一些标志。但是我找不到任何人这样做的例子,使用这些标志会出错。

废话

我只需要启用这个标志:

setFlag(GraphicsItemFlag::ItemSendsGeometryChanges);