Hi, I am trying to write an application to manipulate images using the GraphicsView framework.
I derived QGraphicsPixmapItem to display an image and reimplemented mousePressEvent() to draw a circle at the mouse press position on the image.
I want the image to automatically fit to the window when the view is resized while keeping the circle size fixed.
So I set the ItemIgnoresTransformations flag in order not to resize the circle, and converted the mouse press position to the view coordinate before setting it as the circle position.
But the position of the drawn circle is deviated from the mouse press position.

The following is the simplified code of mine.

Qt Code:
  1. #include <QApplication>
  2. #include <QtCore>
  3. #include <QtGui>
  4.  
  5. class MyItem : public QGraphicsPixmapItem
  6. {
  7. public:
  8. MyItem(QGraphicsView *v): view(v)
  9. {
  10. circle = new QGraphicsPathItem(this);
  11. circle->setFlag(QGraphicsItem::ItemIgnoresTransformations);
  12. circle->hide();
  13. }
  14.  
  15. protected:
  16. virtual void mousePressEvent(QGraphicsSceneMouseEvent * event)
  17. {
  18. QPointF pos_view = view->mapFromScene(mapToScene(event->pos()));
  19. const int Radius = 100;
  20. path.addEllipse(pos_view, Radius, Radius);
  21. circle->setPath(path);
  22. circle->show();
  23. update();
  24. }
  25.  
  26. private:
  27. };
  28.  
  29. class MainWindow : public QMainWindow
  30. {
  31. public:
  32. MainWindow()
  33. {
  34. view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  35. view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  36. view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  37.  
  38. setCentralWidget(&view);
  39. setWindowTitle(tr("Image Viewer"));
  40.  
  41. item = new MyItem(&view);
  42. item->setPixmap(QPixmap("test.tif"));
  43. item->setTransformationMode(Qt::SmoothTransformation);
  44.  
  45. scene.addItem(item);
  46. view.setScene(&scene);
  47. }
  48.  
  49. protected:
  50. virtual void resizeEvent(QResizeEvent *event)
  51. {
  52. view.fitInView(item, Qt::KeepAspectRatio);
  53. }
  54.  
  55. private:
  56. MyItem *item;
  57. };
  58.  
  59.  
  60. int main(int argc, char *argv[])
  61. {
  62. QApplication app(argc, argv);
  63. MainWindow mw;
  64. mw.show();
  65. mw.resize(500, 700);
  66. return app.exec();
  67. }
To copy to clipboard, switch view to plain text mode 

Could someone tell me what is wrong with this?