Results 1 to 2 of 2

Thread: how to move QGraphicsItem on the scene

  1. #1
    Join Date
    Jan 2021
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default how to move QGraphicsItem on the scene

    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 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,023
    Thanks
    280
    Thanked 819 Times in 808 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: how to move QGraphicsItem on the scene

    Do you realize that there is a QGraphicsObject class that inherits QGraphicsItem and QObject? You did not need to reinvent that wheel.

    In your itemChange() method, are you sure you are reacting to the correct GraphicsItemChange value?

    And in the example for the QGraphicsItem::itemChange() documentation (which you mostly implemented), you left out one crucial detail: inside the if() clause, the example method returns a QVariant containing the new position. Your code does not. Instead, after your if() clause exits, it calls the base class method which returns an empty QVariant. So the net effect of your code is to tell the framework that you haven't handled the call at all.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Not able to move QGraphicsEllipseItem on the scene
    By alex.agape94 in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2017, 08:14
  2. Replies: 5
    Last Post: 16th April 2014, 15:45
  3. Replies: 2
    Last Post: 27th November 2012, 10:23
  4. Overload QGraphicsItem Move in Scene
    By D Cell in forum Newbie
    Replies: 2
    Last Post: 16th March 2010, 05:14
  5. Can't move Items in the scene
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2008, 09:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.