Results 1 to 5 of 5

Thread: Create a graphics item on mouse press and put it directly into drag/move mode

  1. #1
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Create a graphics item on mouse press and put it directly into drag/move mode

    Hi all,

    I have a graphics view and I want a fairly simple feature. When I click on a graphics item I want to create a new movable graphics item and want this item to be moved directly.

    Right now, I have implemented the mousePressEvent() and create the new item. But I always need to click a second time (and hold the mouse button and move), in order to get a call to itemChanged() with QGraphicsItem::ItemPositionChange.

    How can I get the QGraphicsView framework to recognize the currently pressed mouse button as start of a drag operation on the new item?

    Thanks,
    Andreas
    Andreas

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Create a graphics item on mouse press and put it directly into drag/move mode

    I think your problem is that when the mouse button goes down, there is no graphics item present yet, so the scene / view has no currently selected item. Perhaps you need to add some code to select the item immediately after creating it.
    <=== 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.

  3. #3
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Create a graphics item on mouse press and put it directly into drag/move mode

    Selecting the item is easy, but I want the item to be marked by the framework as "started a drag operation" so subsequent mouse movements will make the graphics scene send the itemChanged() event with type QGraphicsItem::ItemPositionChange.

    My best bet is to create and emit a new graphics view "mouse pressed" event. But currently that doesn't seem to cause anything. Maybe I need to use a QTimer::singleshot(), so that the last event gets completed before sending the new?

    Any ideas?
    Andreas

  4. #4
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Create a graphics item on mouse press and put it directly into drag/move mode

    Update: I have tried two things without avail:

    1. after creating the object calling "grabMouse()" on the object (still within the QGraphicsScene::mousePressEvent() event handler function). Does not work because base class implementation ungrabs the item right away.

    2. sending an own MousePressEvent to the graphics view using the following code:

    Qt Code:
    1. QGraphicsView* view = views()[0];
    2. QPointF ptScene = mouseEvent->pos(); // mouseEvent->pos() holds the current position that the user clicked on
    3. // Note: the newly created item has a boundingRect() that encloses this position.
    4. QPoint ptView = view->mapFromScene(ptScene);
    5. QPoint ptGlobal = view->viewport()->mapToGlobal(ptView);
    6.  
    7. QGraphicsSceneMouseEvent event2(QEvent::GraphicsSceneMousePress);
    8. event2.setScenePos(ptScene);
    9. event2.setPos(ptScene);
    10. event2.setScreenPos(ptGlobal);
    11. event2.setButton(Qt::LeftButton);
    12. event2.setButtons(Qt::LeftButton);
    13. event2.setModifiers(QApplication::keyboardModifiers());
    14.  
    15. qApp->sendEvent(view, &event2); // strange: does not call the QGraphicsScene::mousePressEvent() function again!
    To copy to clipboard, switch view to plain text mode 

    So, in variant 2, maybe the mouse press event object is not fully configured? Behavior does not change if I delay sending the event by a timer.

    Problem remains: creating an object on click and putting it into drag mode right away...???


    Added after 21 minutes:


    Solved:

    1. create new item
    2. set flags: setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
    3. add to scene
    4. call grabMouse() on newly created item

    Do not send another event!

    This appears to work.

    Note: you may call setSelected(true) when creating the item as well. However, on first move the item will get selected anyway.
    Last edited by ghorwin; 2nd September 2019 at 14:29.
    Andreas

  5. The following user says thank you to ghorwin for this useful post:

    d_stranz (2nd September 2019)

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Create a graphics item on mouse press and put it directly into drag/move mode

    This appears to work.
    Great, glad you solved it. Thanks for posting your solution - this will certainly help others. I will probably be doing something similar soon, so it is good to have this to refer to.
    <=== 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. Linux, disable alt+left mouse press drag
    By aftos in forum Qt Programming
    Replies: 0
    Last Post: 21st March 2014, 09:43
  2. Replies: 3
    Last Post: 22nd February 2013, 20:56
  3. Find the item under the mouse press
    By sajis997 in forum Newbie
    Replies: 5
    Last Post: 18th August 2011, 14:24
  4. Replies: 1
    Last Post: 17th August 2011, 18:44
  5. Graphics item position after drag
    By Miihkali in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2009, 11:01

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.