Hello, I made an application using the Qt and c++, I'm using QGraphicsView to display a map consist of different map tiles added to QGraphicsScene as QGraphicsItem. I also add some waypoints as QGraphicsItem to my map with zValue of 2 so they display on top of my tiles, also I add these way points to a list so I can use them later .until now everything works well, but what I need to do is to be able to move these way points by pressing Ctrl + Mouse Left-Click Button and emit a signal with their new position so I can update these way points in my list. so far I've tried different method like reimplementing itemChange or mouseMoveEvent, but none of them works well. I can't even move the item. I also set related flags but still not work, I appreciate if you can help me with it.

.h:

Qt Code:
  1. #include <QGraphicsItem>
  2. #include <QMouseEvent>
  3. #include <QPainter>
  4.  
  5.  
  6. class wayPointsConstruct:public QObject, public QGraphicsItem
  7. {
  8.  
  9. Q_OBJECT
  10. public:
  11.  
  12.  
  13. explicit wayPointsConstruct(QPointF,double alt, double spd, int cntr,QGraphicsItem *parent = nullptr);
  14.  
  15. QRectF boundingRect() const override;
  16. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
  17.  
  18. double altitude=0;
  19. double speed=0;
  20. double longitude;
  21. double latitude;
  22. int counter;
  23.  
  24. protected:
  25.  
  26. QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
  27.  
  28.  
  29. signals:
  30.  
  31. void wayPointMoves(QPointF);
  32.  
  33.  
  34.  
  35.  
  36. };
To copy to clipboard, switch view to plain text mode 


.cpp:

Qt Code:
  1. #include "waypointsconstruct.h"
  2. #include <QFont>
  3. #include <QGraphicsView>
  4. #include <QGraphicsSceneDragDropEvent>
  5. #include <QApplication>
  6. #include <QDebug>
  7.  
  8.  
  9. wayPointsConstruct::wayPointsConstruct(QPointF lonLat, double alt, double spd, int cntr,QGraphicsItem * ){
  10.  
  11.  
  12. setFlag(QGraphicsItem::ItemIsSelectable);
  13. setFlag(QGraphicsItem::ItemIsMovable);
  14. setFlag(QGraphicsItem::ItemSendsGeometryChanges);
  15. setFlag(QGraphicsItem::ItemSendsScenePositionChanges );
  16.  
  17.  
  18. setAcceptHoverEvents( true );
  19.  
  20.  
  21. this->setZValue(2);
  22.  
  23. longitude= lonLat.x();
  24. latitude= lonLat.y();
  25. altitude=alt;
  26. speed= spd;
  27. counter= cntr;
  28.  
  29.  
  30. }
  31.  
  32. QRectF wayPointsConstruct::boundingRect() const
  33. {
  34. return QRectF(0, 0, 300, 300); //size can change
  35. }
  36.  
  37.  
  38. void wayPointsConstruct::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
  39. {
  40. painter->setRenderHint(QPainter::HighQualityAntialiasing);
  41.  
  42. QPixmap pixmap(":/Icons/Icons/WpHereNotVerified.png");
  43. QPixmap scaled = pixmap.scaled(QSize(50,50),Qt::KeepAspectRatio);
  44.  
  45. painter->drawPixmap(-23,-50,scaled);
  46.  
  47.  
  48. QFont font;
  49. font.setBold(true);
  50. painter->setFont(font);
  51. painter->setPen(QPen(Qt::red, 0));
  52. painter->drawText(-23,10,QString::number(counter));
  53. painter->drawText(-23,20,QString("ALT: %1").arg(QString::number(altitude)));
  54. painter->drawText(-23,30,QString("Speed: %1").arg(QString::number(speed)));
  55.  
  56.  
  57.  
  58. }
  59.  
  60. QVariant wayPointsConstruct::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
  61. {
  62.  
  63. if (change == ItemPositionChange && scene()) {
  64. // value is the new position.
  65. QPointF newPos = value.toPointF();
  66. emit wayPointMoves(mapToScene(newPos));
  67. }
  68. return QGraphicsItem::itemChange(change, value);
  69. }
To copy to clipboard, switch view to plain text mode