Results 1 to 4 of 4

Thread: Not able to move QGraphicsEllipseItem on the scene

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.