Item.h:

Qt Code:
  1. class Item:public QGraphicsRectItem
  2. {
  3. public:
  4. Item(const QRectF & rect, QGraphicsItem * parent = 0);
  5. void mousePressEvent(QGraphicsSceneMouseEvent * event);
  6. void ​mouseMoveEvent(QGraphicsSceneMouseEvent * event);
  7. void ​mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
  8. ~Item();
  9. };
To copy to clipboard, switch view to plain text mode 
Item.cpp:
Qt Code:
  1. Item::Item(const QRectF & rect, QGraphicsItem * parent)
  2. :QGraphicsRectItem(rect, parent)
  3. {
  4. }
  5.  
  6. void Item::mousePressEvent(QGraphicsSceneMouseEvent * event){
  7. qDebug("press");
  8. QGraphicsRectItem::mousePressEvent(event);
  9. event->accept();
  10. }
  11. void Item::​mouseMoveEvent(QGraphicsSceneMouseEvent * event){
  12. qDebug("move");
  13. }
  14.  
  15. Item::~Item(){}
To copy to clipboard, switch view to plain text mode 
main.cpp:
Qt Code:
  1. #include <QApplication>
  2. #include "QGraphicsView"
  3. #include "QGraphicsScene"
  4. #include "item.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QApplication a(argc, argv);
  9. // MainWindow w;
  10. // w.show();
  11.  
  12. auto item = new Item(QRectF(0,0,100,100));
  13.  
  14. auto scene = new QGraphicsScene;
  15. scene->addItem(item);
  16.  
  17. auto view = new QGraphicsView(scene);
  18. view->show();
  19.  
  20. return a.exec();
  21. }
To copy to clipboard, switch view to plain text mode 

I have already explicitly call accept() after calling the base class implementation in the mousePressEvent(). But it doesn't work