Hi,

I am trying to get the positions of the graphicsitems in the scene.
But their QPointF value always remains (0,0).

I am painting when mouse-click event occurs. On debugging scene->items(), I get

(QGraphicsItem(this =0x22edff0, parent =0x0, pos =QPointF(0, 0) , z = 0 , flags = ( ) ) )

for each graphics item in scene but with different memory address.

This is my mainwindow.cpp code:
Qt Code:
  1. #include "mainwindow.h"
  2. #include <QDebug>
  3.  
  4. MainWindow::MainWindow()
  5. {
  6. scene = new QGraphicsScene;
  7. view = new QGraphicsView;
  8. view->setScene(scene);
  9. button = new QPushButton("Item");
  10. QGridLayout *layout = new QGridLayout;
  11. layout->addWidget(button);
  12. layout->addWidget(view);
  13. setLayout(layout);
  14. connect(button, SIGNAL(clicked()), this, SLOT(createItem()));
  15. }
  16.  
  17. void MainWindow::createItem()
  18. {
  19. myEntity = new Item;
  20. scene->addItem(myEntity);
  21. count_items();
  22. }
  23.  
  24. void MainWindow::count_items()
  25. {
  26. qDebug() << scene->items().count();
  27. qDebug() << scene->items();
  28. }
  29.  
  30. MainWindow::~MainWindow()
  31. {
  32. }
To copy to clipboard, switch view to plain text mode 

This is my item.cpp code:
Qt Code:
  1. #include "item.h"
  2.  
  3. Item::Item()
  4. {
  5. ClickFlag = true;
  6. PaintFlag = false;
  7. }
  8.  
  9. Item::~Item()
  10. {
  11. }
  12.  
  13. QRectF Item::boundingRect() const
  14. {
  15. // outer most edges
  16. return QRectF(0,0,1450,1400);
  17. }
  18. void Item::mousePressEvent(QGraphicsSceneMouseEvent *event)
  19. {
  20. if(event->button()==Qt::LeftButton){
  21. if(ClickFlag){
  22. x = event->pos().x();
  23. y = event->pos().y();
  24. PaintFlag = true;
  25. ClickFlag = false;
  26. }
  27. }
  28. }
  29.  
  30. void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
  31. QWidget *widget)
  32. {
  33. if(PaintFlag){
  34. QPen paintPen;
  35. paintPen.setWidth(4);
  36. pt.setX(x);
  37. pt.setY(y);
  38. painter->setPen(paintPen);
  39. painter->drawPoint(x,y);
  40. update();
  41. }
  42. }
To copy to clipboard, switch view to plain text mode 

I can't seem to find the position of these items correctly.