QGraphicsItem移动事件-获取绝对位置 [英] QGraphicsItem move event - get absolute position

查看:1067
本文介绍了QGraphicsItem移动事件-获取绝对位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QGraphicsEllipseItem 我想移动并在移动时触发信号。
因此,我将 QGraphicsEllipseItem QObject 分为子类,并覆盖了 itemChange 触发信号的方法。一切似乎都有效,但是报告的位置似乎与该项目的旧位置有关。即使询问项目的位置似乎仅是要检索相对坐标。

I have a QGraphicsEllipseItem that I want to be movable and triggering signals on moving. So I subclassed QGraphicsEllipseItem and QObject and overrode the itemChange method to trigger a signal. That all seems to work but the positions that are reported seem to be relative to the old position of the item. Even if asking the item for its position seems only to retrieve the relative coordinates.

这里有一些代码可以明确说明我所做的事情:

Here is some code to make clear what I have done:

class MyGraphicsEllipseItem: public QObject, public QGraphicsEllipseItem
{
  Q_OBJECT

public:

  MyGraphicsEllipseItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = 0, QGraphicsScene *scene = 0)
    :QGraphicsEllipseItem(x,y,w,h, parent, scene)
  {}

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

signals:
  void itemMoved(QPointF p);
};

QVariant MyGraphicsEllipseItem::itemChange( GraphicsItemChange change, const QVariant  &value )
{ 
  // value seems to contain position relative start of moving
  if (change == ItemPositionChange){
    emit itemMoved(value.toPointF());
  }
  return QGraphicsEllipseItem::itemChange(change, value); // i allso tried to call this before the emiting
}

之前调用此命令:

  MyGraphicsEllipseItem* ellipse = new MyGraphicsEllipseItem(someX, someY, someW, someH);
  ellipse->setFlag(QGraphicsItem::ItemIsMovable, true);
  ellipse->setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
  connect(ellipse, SIGNAL(itemMoved(QPointF)), SLOT(on_itemMoved(QPointF)));
  graphicsView->scene()->addItem(ellipse);

和广告位:

void MainWindow::on_itemMoved( QPointF p)
{
  MyGraphicsEllipseItem* el = dynamic_cast<MyGraphicsEllipseItem*>(QObject::sender());
  QPointF newPos = el->scenePos();
  scaleLbl->setText(QString("(%1, %2) - (%3, %4)").arg(newPos.x()).arg(newPos.y()).arg(p.x()).arg(p.y()));
}

奇怪的是, newpos p 几乎相等,但包含相对于运动开始的坐标。

The strange thing is that newpos and p are almost equal but contain coordinates relative to the beginning of the movement.

我如何获取被拖动对象的当前位置?还有另一种方法可以实现目标吗?

How do I get the current position of the dragged object? Is there another way to achieve the goal?

推荐答案

我发现了原因:
构造函数 QGraphicsEllipseItem :: QGraphicsEllipseItem(qreal x,qreal y,qreal width,qreal height,QGraphicsItem * parent = 0)不能按预期工作。用x和y调用它后,该项目仍报告其位置为0,0。给构造函数0,0并使用 setPos(x,y)显式设置位置即可解决问题。

I found the reason: the constructor QGraphicsEllipseItem::QGraphicsEllipseItem ( qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0 ) does not work as expected. after calling it with some x and y the item still reports 0,0 as its position. giving 0,0 to constructor and explicitly setting position with setPos(x,y) solves the thing.

I真的很奇怪,这是这种行为的意图。该文档对此没有任何提示。

I really wonder that was the intention for this behaviour. The documentation gives no hint for this.

这篇关于QGraphicsItem移动事件-获取绝对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆