Results 1 to 4 of 4

Thread: Not able to move QGraphicsEllipseItem on the scene

  1. #1
    Join Date
    Sep 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question Not able to move QGraphicsEllipseItem on the scene

    Hi guys!
    I've been trying to solve this problem for a couple of days now, and I can't seem to be able to find a solution for it.
    Basically I have my own QGraphicsView, with my own QGraphicsScene and my own QGraphicsEllipseItem. I want to be able to move the Ellipse in the scene.
    Until now I tried using the mouse events and the itemChange functions, but with no success.
    In short, I want to be able to move the points in my scene only on the y-axis. Using the mouse events I was able to move the points, but they were all over the place. With the itemChange function they don't move at all.
    Any ideas?


    Here is some code:

    The QGraphicsEllipseItem class implementation:

    Qt Code:
    1. PunctGrafic::PunctGrafic(int x, int y, QGraphicsItem *parent)
    2. {
    3. // this->setParentItem(parent);
    4. QPen pen;
    5. pen.setBrush(QBrush(Qt::SolidPattern));
    6. pen.setWidth(3);
    7. pen.setColor(QColor(Qt::red));
    8. this->setPen(pen);
    9. this->setRect(x - 1.5 , y - 1.5 , 3 , 3);
    10. setFlag(QGraphicsItem::ItemIsSelectable,true);
    11. setFlag(QGraphicsItem::ItemIsMovable,true);
    12. setFlag(QGraphicsItem::ItemSendsGeometryChanges,true);
    13. setFlag(QGraphicsItem::ItemSendsScenePositionChanges,true);
    14.  
    15. }
    16.  
    17. QVariant PunctGrafic::itemChange(GraphicsItemChange change, const QVariant &value)
    18. {
    19.  
    20. if (change == QGraphicsItem::ItemPositionChange)
    21. return QPointF(pos().x(), value.toPointF().y());
    22. return QGraphicsItem::itemChange(change, value);
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    and here is what i tried to do with the mouse events, but to no success:

    Qt Code:
    1. void PunctGrafic::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if(event->button() == Qt::LeftButton)
    4. {
    5. //moveStarted = true;
    6. this->setCursor(QCursor(Qt::ClosedHandCursor));
    7. QGraphicsItem::mousePressEvent(event);
    8. event->accept();
    9. return;
    10. }
    11. event->ignore();
    12. }
    13. ////---------------------------------------------------------------------------------------------
    14. void PunctGrafic::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    15. {
    16. if(event->button() == Qt::LeftButton)
    17. {/*
    18.   int g =
    19.   int h = mapToScene(event->pos()).y();*/
    20. this->setPos(mapToItem(parentItem(),mapToParent(event->pos()).x(),mapToParent(event->pos()).y()));
    21. QGraphicsItem::mouseMoveEvent(event);
    22.  
    23. event->accept();
    24. return;
    25. }
    26. event->ignore();
    27. }
    28. ////---------------------------------------------------------------------------------------------
    29. void PunctGrafic::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    30. {
    31. if (event->button() == Qt::LeftButton)
    32. {
    33. //moveStarted = false;
    34. this->setCursor(QCursor(Qt::ArrowCursor));
    35. QGraphicsItem::mouseReleaseEvent(event);
    36. event->accept();
    37. return;
    38. }
    39. event->ignore();
    40. }
    41. //---------------------------------------------------------------------------------------------
    To copy to clipboard, switch view to plain text mode 

    Thank you!
    Attached Images Attached Images
    Last edited by alex.agape94; 5th September 2017 at 08:48.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Not able to move QGraphicsEllipseItem on the scene

    Hi, if you want to move the item only in Y direction, why do you change the X position?
    this->setPos(mapToItem(parentItem(),mapToParent(event->pos()).x(),mapToParent(event->pos()).y()));

    Ginsengelf

  3. #3
    Join Date
    Sep 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Not able to move QGraphicsEllipseItem on the scene

    Thank you for responding!

    The issue is that they don't move at all. I was thinking that it has to be a flag or something that I forgot or something.
    I simply don't understand why they refuse to move. I tried debugging, and when I click an item, the itemChange() function is being called, but it doesn't enter the if statement with change == QGraphicsItem::ItemPositionChange . In the debugging window, change = ItemClickChange or something like that, so it just sees the click, not the move event. I don't know why...

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Not able to move QGraphicsEllipseItem on the scene

    No need intercept mouse events.
    Qt Code:
    1. class EllipseItem : public QGraphicsEllipseItem
    2. {
    3. public:
    4. EllipseItem(const QRectF & rect, QGraphicsEllipseItem * parent = nullptr)
    5. : QGraphicsEllipseItem(rect, parent)
    6. {
    7. setFlags(ItemIsMovable | ItemSendsGeometryChanges);
    8. }
    9.  
    10. protected:
    11. QVariant itemChange(GraphicsItemChange change, const QVariant & value) override
    12. {
    13. if(change == ItemPositionChange)
    14. return QPointF(scenePos().x(), value.toPointF().y());
    15.  
    16. return QGraphicsEllipseItem::itemChange(change, value);
    17. }
    18. };
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 5
    Last Post: 16th April 2014, 15:45
  2. Replies: 2
    Last Post: 27th November 2012, 10:23
  3. Replies: 4
    Last Post: 27th September 2010, 11:28
  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

Tags for this Thread

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.