Results 1 to 8 of 8

Thread: move GraphicsItems within a GraphicsScene

  1. #1
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question move GraphicsItems within a GraphicsScene

    Hello,

    I have got the following task, which I cannot achieve since a couple of days:

    I have inserten several circles (GraphicsItems) into my scene and after that I would like to change their positions within this scene by dragging them (left mouse button + moving the mouse). I have read almost all information about Drag & Drop, but I still cannot get my items moved through the scene. I have implemented my own class inherited from QGraphicsItem and overriden the functions MousePressEvent() MouseMoveEvent() and MouseReleaseEvent() for this class and after that I also implemented my QGraphicsScene class because I wanted to override the functions DropEvent() and DragEnterEvent(). Actually, I guess that there must be an easier solution without Drag & Drop because the Item is already on the scene, so it is not necessary to drop it there again, right?

    The thing I want to do is the following: I create a vector of some points (x and y coordinates) and write the vector elements into the position of generated QGraphicsItems by ( ->setPos() ). During the execution the user should be able to move this items on the scene and thus change the x and y values within the vector elements and the items should remain on the position, where mouse dragging was released, of course.
    Help me please to realize this aim.

    Thank you

    best regards,

    Vitali

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: move GraphicsItems within a GraphicsScene

    You are confusing two approaches here. Drag&drop is one thing and moving items with a mouse is another thing. If you want your items to be movable, all you need to do is to call this on your item:
    Qt Code:
    1. item->setFlag(QGraphicsItem::ItemIsMovable);
    To copy to clipboard, switch view to plain text mode 

    You don't need to reimplement any events or anything.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: move GraphicsItems within a GraphicsScene

    Thank you for reply.

    this flag is set in the constructor of my QGraphicsItem ( setFlag(ItemIsMovable, true) ) and if I dont override all the functions, I have mentioned, then my item can be moved and it remains on the place where I release my mouse button, BUT I also want to get the new position of the moved item in order to update my vector with the x and y coordinates of points. How can I do that then?

    best regards,

    Vitali

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: move GraphicsItems within a GraphicsScene

    Make sure that you call the base class implementations of those methods from your reimplementations.

    But actually you should handle this situation in a different way. Instead of reimplementing mouse operations, reimplement QGraphicsItem::itemChange() and update your data from within there. This way you make sure the data will get updated regardless of the way the item is moved on the scene.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: move GraphicsItems within a GraphicsScene

    Thank you for the advice.
    Now I use the itemChange() function and it seems to work, but now I think how to detect index of the moved GraphicsItem and the index of the scene, so that I can update the coordinates within my point vectors.
    I have got a
    Qt Code:
    1. QVector<QGraphicsScene *> vec_scenes;
    To copy to clipboard, switch view to plain text mode 
    , where I pushed back several scenes. Every scene corresponds to a construct(individual) consisting of two vectors (one for x and another for y coordinates). With this I can then add a certain amount of points (graphicsitems) into the scene for every construct. I have got more than one construct (individuals) within a vector of individuals, so it is like a population. Don't be surprised, I'm working on evolutionary algorithms and want to visualize the process by means of Qt.

    So, how can I update the x, y coordinates of an individual , if a graphicsitem within a scene will be moved? Any ideas?

    best regards

  6. #6
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: move GraphicsItems within a GraphicsScene

    Now I use the itemChange() function and it seems to work, but now I think how to detect index of the moved GraphicsItem and the index of the scene
    You can have a QHash (index, pointer) of scenes and search them by id.

    So, if you move a QGraphicsItem it's event itemChange(GraphicsItemChange change, const QVariant &value), is called (change == ItemPositionHasChanged). So you can get it's id, and send it whereever you want. You can keep the ID of the scene on each qgraphics item or, better, a pointer.

  7. #7
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: move GraphicsItems within a GraphicsScene

    I found out how to find the index of a point within a scene, namely with the following code:
    Qt Code:
    1. QList<QGraphicsItem *> lst = this->scene()->items();
    2. QList<QGraphicsItem *>::iterator it = lst.begin();
    3. unsigned i=0;
    4. while (it != lst.end()) {
    5. if((*it) == this){
    6. //(*it)->hide();
    7. qDebug() << "index: " << i;
    8. }
    9. ++it;
    10. ++i;
    11. }
    To copy to clipboard, switch view to plain text mode 
    and it works, but then I tried to determine the index of the corresponding scene and it does not work. I think it has something to do with the access to a QVector of another class:
    Qt Code:
    1. QVector<QGraphicsScene *> scenesPhen = main.getScenes();
    2. QVector<QGraphicsScene *>::iterator it2 = scenesPhen.begin();
    3. qDebug() << "scenesPhen.size() = " << scenesPhen.size();
    4. unsigned j=0;
    5. while (it2 != scenesPhen.end()) {
    6. cout << "(*it2): " << (*it2) << "\t\t scene(): "<< scene()<< endl;
    7. if((*it2) == scene()){
    8. qDebug() << "index of scene: " << j;
    9. }
    10. ++it2;
    11. ++j;
    12. }
    To copy to clipboard, switch view to plain text mode 
    hereby is "main" another class, declared in header file and "getScenes()" is the public function of another class, which gives by the QVector<QGraphicsScene*>.

    The cout output gives everytime another combination of addresses, if I move items on different scenes, but actually the addresses of scenes within the scene-vector should remain the same, right? So I guess, that here is somewhere my mistake. But where? Please help...
    Thank you

  8. #8
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: move GraphicsItems within a GraphicsScene

    my last post is solved... My fault was just to use
    Qt Code:
    1. mainFrame main;
    To copy to clipboard, switch view to plain text mode 
    in the header file and then to use it within the cpp file, but then it has nothing to do with the existing mainFrame class, thus all addresses were wrong.
    Now I give by the pointer to the mainFrame class in the constructor of Vertex class, so that it has the right pointer at running time.

    regards,

    Vitali

Similar Threads

  1. How to move child widget with parent widget?
    By anupamgee in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 15:23
  2. Replies: 1
    Last Post: 22nd November 2008, 06:32
  3. How to move an item on a GraphicsScene
    By Holy in forum Qt Programming
    Replies: 17
    Last Post: 25th July 2008, 14:40
  4. Move and resize with layout
    By waediowa in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2008, 08:16
  5. Move Rectangle on mouse Move
    By vermarajeev in forum Qt Programming
    Replies: 24
    Last Post: 14th May 2007, 05:34

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.