Results 1 to 6 of 6

Thread: Moving Items with itemChange?

  1. #1

    Default Moving Items with itemChange?

    Hi,
    I have problems with itemChange. I use it like the example "Diagram Scene".
    I have two ellipse and a line between them and if I move the ellipse the line should move too.
    ellipseitem.cpp
    Qt Code:
    1. QVariant EllipseItem::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. if (change == QGraphicsItem::ItemPositionChange)
    4. {
    5. foreach (LineItem *arrow, arrows)
    6. {
    7. arrow->updatePosition();
    8. }
    9. }
    10. return value;
    11. }
    To copy to clipboard, switch view to plain text mode 
    ellipseitem.h
    Qt Code:
    1. ...
    2. protected:
    3. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    4. private:
    5. QList<LineItem *> arrows;
    To copy to clipboard, switch view to plain text mode 

    but I get
    LineItem was not declared in this scope
    C++ forbids declaration of arrows with no type

    My LineItem is like this: LineItem(qreal x1, qreal y1, qreal x2, qreal y2);
    Can somebody tell me whats wrong?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Moving Items with itemChange?

    did you #include your LineItem in the .cpp file?

  3. #3

    Default Re: Moving Items with itemChange?

    Yes I did. That's why I don't understand why it is not declared...

  4. #4

    Default Re: Moving Items with itemChange?

    no idea? Please help...

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Moving Items with itemChange?

    can u show some more code..
    and which line exactly are u getting the error ?

  6. #6

    Default Re: Moving Items with itemChange?

    In my scene I only make an ellipse that is movable when I use the mouse.
    ellipseitem.h
    Qt Code:
    1. class EllipseItem : public QGraphicsEllipseItem
    2. {
    3. public:
    4. EllipseItem(qreal x,qreal y,qreal w,qreal h);
    5. ~EllipseItem();
    6. protected:
    7. void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
    8. void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
    9. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    10. private:
    11. QPointF posi;
    12. QPointF endi;
    13. QList<LineItem *> arrows;
    14. };
    To copy to clipboard, switch view to plain text mode 
    ellipseitem.cpp
    Qt Code:
    1. EllipseItem::EllipseItem(qreal x,qreal y,qreal w,qreal h)
    2. : QGraphicsEllipseItem(x,y,w,h) {}
    3.  
    4. EllipseItem::~EllipseItem() {}
    5.  
    6. void EllipseItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
    7. {
    8. posi = e->scenePos();
    9. setRect(posi.x()-5, posi.y()-5, 10, 10);
    10. endi=e->scenePos();
    11. setRect(endi.x()-5, endi.y()-5, 10, 10);
    12. }
    13.  
    14. void EllipseItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
    15. {
    16. endi=e->scenePos();
    17.  
    18. if(scene()->itemAt(endi))
    19. { LineItem *linie = new LineItem(posi.x(), posi.y(), endi.x(), endi.y());
    20. scene()->addItem(linie);
    21. }
    22. else {QGraphicsEllipseItem::mouseReleaseEvent(e);}
    23. }
    24.  
    25. QVariant EllipseItem::itemChange(GraphicsItemChange change, const QVariant &value)
    26. {
    27.  
    28. if (change == QGraphicsItem::ItemPositionChange)
    29. {
    30. foreach (LineItem *arrow, arrows)
    31. {
    32. arrow->updatePosition();
    33. }
    34. }
    35. return value;
    36. }
    To copy to clipboard, switch view to plain text mode 
    lineitem.cpp
    Qt Code:
    1. LineItem::LineItem(qreal x1, qreal y1, qreal x2, qreal y2)
    2. : QGraphicsLineItem(x1, y1, x2, y2) {}
    3.  
    4. LineItem::~LineItem() {}
    5.  
    6. void LineItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
    7. { startpkt = e->scenePos(); }
    8.  
    9. void LineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
    10. {
    11. endpkt = e->scenePos();
    12. if(scene()->itemAt(endpkt))
    13. { setLine(QLineF(startpkt.x(), startpkt.y(), endpkt.x(), endpkt.y())); }
    14. }
    15.  
    16. void LineItem::updatePosition()
    17. { QLineF line(startpkt.x(), startpkt.y(), endpkt.x(), endpkt.y());
    18. setLine(line);
    19. }
    20.  
    21. void LineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
    22. {
    23. EllipseItem *myStartItem = new EllipseItem(startpkt.x(), startpkt.y(),10,10);
    24. EllipseItem *myEndItem = new EllipseItem(endpkt.x(), endpkt.y(),10,10);
    25.  
    26. if (myStartItem->collidesWithItem(myEndItem))
    27. return;
    28. else
    29. { painter->drawLine(QLineF(startpkt.x(), startpkt.y(), endpkt.x(), endpkt.y())); }
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 
    Can you see the mistake?

Similar Threads

  1. (PyQt) moving items within a QTreeWidget
    By calireno in forum Newbie
    Replies: 0
    Last Post: 12th November 2008, 04:33
  2. Moving items within a model
    By iswm in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2007, 09:29
  3. Moving items between two views.
    By YuriyRusinov in forum Qt Programming
    Replies: 11
    Last Post: 2nd April 2007, 14:53
  4. Selective highlighting of Items
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 13:20
  5. moving Qlistview items
    By :db:sStrong in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2006, 13:25

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.