Results 1 to 11 of 11

Thread: QGraphicsScene drag event

  1. #1
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QGraphicsScene drag event

    hello,

    I'm trying to catch the drag event in QGraphicsScene so I can draw when dragging the mouse. There is such a function in the API so it should be possible:
    http://doc.trolltech.com/4.4/qgraphi...dragEnterEvent

    But I can't seem to trigger this event. I can catch mouse move, click, and release, but not drag enter or drag move.

    There seems to be a lot of confusion on this topic because there are many threads about it with conflicting information.
    In this thread wysota says the event can be overloaded but aamer4yu says it can't (and they are both experts).
    In this thread some suggest that qgraphicsitem needs to be subclassed instead.
    In this thread subclassing the event seems to have worked and it was suggested setting the drag mode property in qgraphicsview.

    So, how would you suggest I catch mouse drag events so I can draw in the scene?

    thanks,
    Richard

  2. #2
    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: QGraphicsScene drag event

    Referring to the first thread link... I guess i had expressed myself clearly and wysota had misunderstood.

    Anyways, coming to your post..
    I'm trying to catch the drag event in QGraphicsScene so I can draw when dragging the mouse.
    What do you want to draw ?
    If you want to draw the item itself, you dont have to implement drag functionality yourself. You just need to call QGraphicsItem::setFlags(QGraphicsItem::ItemIsMovab le);

    If you can elaborate more on what you want to achieve, it wud be better.

  3. #3
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene drag event

    By draw I mean like for a paint program. I want to annotate a background image so if I could catch the drag event then I would call addLine()
    http://doc.trolltech.com/4.4/qgraphi...e.html#addLine

  4. #4
    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: QGraphicsScene drag event

    If thats the case, drag events are not relevant.
    You will need to override mousePress, mouseMove and mouseRelease events of the scene.
    You can contruct a polygon out of the move events, and add a item as u like to the scene.

    Also if only drawing is your aim, you wont even need graphics scene or view. You can simply draw on a widget. See Scribble example under Widgets in Qt Demo.

    Hope this helps

  5. #5
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene drag event

    Thanks for your advice aamer4yu.
    Unfortunately I found mousemove doesn't get called when the mouse button is down. That's why I'm trying the drag events.
    Do you know if the scene drag events can be made to work?

    Also if only drawing is your aim, you wont even need graphics scene or view. You can simply draw on a widget.
    I want to keep the background image separate from the drawing so I can support erase efficiently. And the scene/view stack supports zooming.

    Could I get erase and zoom working efficiently on a widget?
    Last edited by rbp; 11th February 2009 at 11:27.

  6. #6
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene drag event

    I figured it out!!

    To support drag events for QGraphicsScene you must allow drops on the scene
    Qt Code:
    1. setAcceptDrops(true);
    To copy to clipboard, switch view to plain text mode 

    create a QDrag object with mime data in the mousePressEvent()
    Qt Code:
    1. QDrag *drag = new QDrag(this);
    2. drag->setMimeData(new QMimeData()); // does not work without mime data
    3. drag->exec();
    To copy to clipboard, switch view to plain text mode 

    and accept the drag event in dragEnterEvent()
    Qt Code:
    1. void dragEnterEvent(QDragEnterEvent *event) {
    2. event->acceptProposedAction();
    3. }
    To copy to clipboard, switch view to plain text mode 

    Then you can catch the dragMoveEvent()

    Phew - that's a lot of work. I would certainly prefer if mouseMoveEvent() still worked when the mouse button was down.
    Last edited by rbp; 11th February 2009 at 23:37. Reason: simplified code example

  7. #7
    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: QGraphicsScene drag event

    What functionality are u trying to achieve with your application ?
    I am still not able to understand why you need drag drop handling.

  8. #8
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene drag event

    What functionality are u trying to achieve with your application ?
    you already asked that question and I answered!

    I don't need drag/drop handling but I need to tell when the mouse is moving and the button is down, and for some reason the mousemove event isn't being called when the button is down.
    Anyway, I've got the mouse events I want now with the drag events.
    Thanks for trying to help.

  9. #9
    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: QGraphicsScene drag event

    you already asked that question and I answered!

    I don't need drag/drop handling but I need to tell when the mouse is moving and the button is down, and for some reason the mousemove event isn't being called when the button is down.
    Anyway, I've got the mouse events I want now with the drag events.
    Thanks for trying to help.
    Reply With Quote
    That is fine. But I wanted to know are u trying to move graphics Item ? or are u trying to change color / monitor cursor position of hover over graphics item ?

    You had mentioned you are trying paint like program. And I also mentioned graphics view isnt that necessary for that.
    And without code of what you have tried, it is very difficult to say where you are going wrong.

  10. #10
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene drag event

    That is fine. But I wanted to know are u trying to move graphics Item ? or are u trying to change color / monitor cursor position of hover over graphics item ?
    I was after zoom with scrollbars and separate layers for the background image and foreground annotations. So that's what I have now with the scene/view stack.
    Later I may have to add the ability to adjust control points of an annotation, but hopefully not.

    I've implemented a similar widget in FLTK and it was a pain. Qt is fortunate to have higher level widgets like the graphic scene/view.

    Richard

  11. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsScene drag event

    Hi to all!

    I have some widgets embedded into QGraphicsView via QGraphicsProxies. Now, how do I prevent those widgets from being dragged arround view. They must not be moved. Method setDragMode does not work. Please help!
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Custom event gets not propagated to the top level widget
    By nightghost in forum Qt Programming
    Replies: 0
    Last Post: 29th January 2009, 09:06
  2. Drag & Drop when parent and childs accept drops?
    By gri in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2008, 09:00
  3. QGraphicsScene doesn't enter drag event
    By Radagast in forum Qt Programming
    Replies: 8
    Last Post: 16th June 2008, 17:21
  4. Qt event queue overloading?
    By gct in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2008, 18:39
  5. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41

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.