title: Moving an item with the mouse on QGraphicsView / QGraphicsScene
body: <p>I have a <code>QGraphicsView</code> area that displays some Item items. I would like to have mouse move implemented.</p>
  
  <pre><code>class Item
  {
  public:
      Item();
      void update();
      int x, y; // position
      int z; // order - should correspond also to index in item list
  };
  
  class Collection : public QGraphicsView
  {
  Q_OBJECT
  public:
      Collection(QWidget *parent = 0);
      void update();
      QList&lt;Item&gt; *m_items;
  protected:
      virtual void paintEvent(QPaintEvent * event);
      virtual void mousePressEvent(QMouseEvent * event);
      virtual void mouseReleaseEvent(QMouseEvent *event);
  private:
      QPoint offset;
      int itemMoved;
  };
  </code></pre>
  
  <p>trying:</p>
  
  <pre><code>void Collection::mousePressEvent(QMouseEvent* event)
  {
      Item *currentItem = NULL;
      itemMoved = -1;
      foreach (QGraphicsItem *item, this-&gt;items(event-&gt;pos()))
      {
  
  // never get into this loop since my items are not children of QGraphicsItem
  
          currentItem = dynamic_cast&lt;Item*&gt;(item);
          if (event-&gt;button() == Qt::LeftButton &amp;&amp; currentItem)
          {
              itemMoved = currentItem-&gt;z;
              offset = event-&gt;pos();
          }
          else if (event-&gt;button() == Qt::RightButton)
          {
              // set right click stuff
          }
          else
          {
              event-&gt;ignore();
          }
          break;
      }
  }
  
  void CollectionView::mouseReleaseEvent(QMouseEvent* event)
  {
      if(event-&gt;button() == Qt::LeftButton &amp;&amp; itemMoved &gt; 0)
      {
          m_items-&gt;at(itemMoved).x = event-&gt;pos().x();
          m_items-&gt;at(itemMoved).y = event-&gt;pos().y();
  
  // So far multiple fails:
  
  //    error: assignment of data-member 'Item::x' in read-only structure
  //    error: assignment of data-member 'Item::y' in read-only structure
  //    error: passing 'const Item' as 'this' argument of 'void Item::update()' discards qualifiers
  
          m_items-&gt;at(itemMoved).update();
          update();
      }
  }
  </code></pre>
  
  <p>I would make my <code>Item</code> inherit <code>QGraphicsItem</code> but then I  get errors about virtual functions that I don't know how to define (like <code>boundingRect</code> which would depend on the item contents... could be objects, images, svg, text... for some i know how to get the bounding rectangle but for others really idk)... Is that the only way to do this ?</p>
  
  <p>How can I identify he items at the location of the mouse position, and what is preventing me to change the <code>x</code> and <code>y</code> for the item once mouse is moved ?</p>