Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: QGraphicsItem .. Scene .. View question?

  1. #1
    Join Date
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question QGraphicsItem .. Scene .. View question?

    Hello All,

    Not getting much response in my other thread, simple question...

    Is there any examples of QGraphicsView / Scene / Item with
    drag and drop functionality?

    I've read the help pages again and again but not getting my events
    through to the item.

    Go on, please help. I'll give you a gold star, and if I get it all working
    a certificate too!

    thanks,

    DB

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem .. Scene .. View question?

    see diagramscene example
    http://doc.trolltech.com/4.3/graphic...gramscene.html

    for drag drop of items .

    but for learning QGraphicsView and QGraphicsScene padnavigator is best example to follow .
    /Trolltech/Qt-4.4.3/examples/graphicsview/padnavigator.

    and also take a look at this doc
    http://doc.trolltech.com/4.2/graphicsview.html

    especially

    http://doc.trolltech.com/4.2/graphic...#drag-and-drop
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsItem .. Scene .. View question?

    Hi,

    Thanks for this wagmare, your gold star is in the post.

    I'll go over these with a fine tooth pick, however on the surface they
    dont quite do what I have.

    I've got an item, in a scene in a view. I drag something onto the item,
    I can see the drag and then drop in the view. I see the drop in the scene,
    but I dont see it in the item.

    The item has accept drops as true. It's got the dragEnterEvent, dropEvent
    and dragMoveEvent defined. But I dont see these events.

    Any ideas? (worth another star)

    I do have mousePressEvent and mouseReleaseEvent define, there isn't some
    weird clashing going on here? like you only get one event, a release, or a drop
    and not both?

    thanks agian,

    DB

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem .. Scene .. View question?

    better to use mouseReleaseEvent() because
    "As the view receives a drag, it translates the drag and drop events into a QGraphicsSceneDragDropEvent, which is then forwarded to the scene. The scene takes over scheduling of this event, and sends it to the first item under the mouse cursor that accepts drops."
    from http://doc.trolltech.com/4.2/graphic...#drag-and-drop


    for items drop event
    http://doc.trolltech.com/4.2/qgraphi...setAcceptDrops
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsItem .. Scene .. View question?

    Hello again,

    Thanks for this.

    better to use mouseReleaseEvent() because
    "As the view receives a drag, it translates the drag and drop events into a QGraphicsSceneDragDropEvent, which is then forwarded to the scene. The scene takes over scheduling of this event, and sends it to the first item under the mouse cursor that accepts drops."
    from http://doc.trolltech.com/4.2/graphic...#drag-and-drop
    Why do you say better to use this?

    I want what it says in quotes, I want the first item under the mouse to receive the
    drop event. But it doesn't happen. The item setAcceptDrops(true) is called in the
    item, the scene dropEvent() can see that there is an item under the mouse with
    the itemAt() call.

    With all this the item still doesn't get the event.

    I tried to use mouseReleaseEvent() but that is only called if the mouseEnterEvent()
    is called in that item before hand. I.e. a drag from somewhere else into this item
    does not result in a mouseReleaseEvent() when you release the drop. Makes sense
    I guess.

    But it's the dropEvent() in the item which I'm missing. I'm thinking it's the fault of
    the QGraphicsScene not passing it on to the item. Tried setting focus of both the
    scene and item before exiting out of the scene drop event, but nothing.

    Qt Code:
    1. void myScene::dropEvent(QGraphicsSceneDragDropEvent * event)
    2. {
    3. qDebug() << "scene: dropEvent";
    4.  
    5. // various things to try to get the event to the item...no luck
    6. //event->ignore();
    7. //event->accept();
    8. //event->acceptProposedAction();
    9.  
    10. this->setFocus(Qt::NoFocusReason);
    11.  
    12.  
    13. gi = this->itemAt( event->scenePos() );
    14.  
    15. if (gi)
    16. {
    17. qDebug() << "Scene(dropEvent); there is an item thar";
    18. gi->setFocus(Qt::NoFocusReason);
    19. gi->setSelected(true);
    20. //gi->setAcceptDrops(true);
    21. }
    22.  
    23. QGraphicsScene::dropEvent(event);
    24. }
    To copy to clipboard, switch view to plain text mode 


    DB

  6. #6
    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: QGraphicsItem .. Scene .. View question?

    What do you mean that the item "doesn't get the event"?
    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.


  7. #7
    Join Date
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsItem .. Scene .. View question?

    In the scene it gets the event, and with the qDebug() line I see on the console output

    Qt Code:
    1. void myScene::dropEvent(QGraphicsSceneDragDropEvent * event)
    2. {
    3. qDebug() << "scene: dropEvent";
    To copy to clipboard, switch view to plain text mode 

    'scene: drop Event'

    I have a similar debug in the item dropEvent() method.

    Qt Code:
    1. void MyNode::dropEvent(QGraphicsSceneDragDropEvent * event)
    2. {
    3. qDebug() << "Node: dropEvent";
    4. QGraphicsItem::dropEvent(event);
    5. }
    To copy to clipboard, switch view to plain text mode 

    But I do not see the 'Node: dropEvent' debug, so the QGraphicsItem doesn't get the event.

  8. #8
    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: QGraphicsItem .. Scene .. View question?

    How is the shape() of the item defined? Is there a chance some other item gets the event? What does this return when ran as part of the scene's dropEvent?
    Qt Code:
    1. qDebug() << items(event->scenePos()).count();
    To copy to clipboard, switch view to plain text mode 
    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.


  9. #9
    Join Date
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsItem .. Scene .. View question?

    Hi,

    Thanks for the reply.

    It comes back with '1', which is what I'd expect, only got one item there.

    --

    Have found an interesting thing though...

    In the view, I have dragMoveEvent of :-

    Qt Code:
    1. void myView::dragMoveEvent(QDragMoveEvent *event) {
    2. }
    To copy to clipboard, switch view to plain text mode 

    With this the dropEvent gets to the scene. However, if I add
    Qt Code:
    1. QGraphicsView::dragMoveEvent(event);
    To copy to clipboard, switch view to plain text mode 
    to this method the dropEvent doesn't.

    Also, if I dont have this function defined, it also doesn't get to the
    scene.

    Is this this the correct behaviour?

    Usually when I redefine a method I call the base class method at
    the end like 'Base:SomeMethod();'.

    Thanks again, I appreciate your help,

    DB

  10. #10
    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: QGraphicsItem .. Scene .. View question?

    If you don't call the base class implementation, the scene shouldn't receive the event.
    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.


  11. #11
    Join Date
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Re: QGraphicsItem .. Scene .. View question?

    Hello Again!

    If you don't call the base class implementation, the scene shouldn't receive the event.
    OK, so there's the problem? I see the exact opposite of this.

    Shall we have a quick overview of my code -

    View header file

    Qt Code:
    1. class myView : public QGraphicsView
    2. {
    3. public:
    4. myView(QWidget *parent=0);
    5. protected:
    6.  
    7. void dragEnterEvent(QDragEnterEvent *event);
    8. void dropEvent(QDropEvent *event);
    9. void dragMoveEvent(QDragMoveEvent *event);
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    view methods

    Qt Code:
    1. myView::myView(QWidget *parent) : QGraphicsView(parent) {
    2. // setAcceptDrops(true);
    3. // Comment the line below, and drop starts working
    4. setScene(new QGraphicsScene(this));
    5. }
    6.  
    7. void myView::dragEnterEvent(QDragEnterEvent *event) {
    8. // event->accept();
    9. qDebug() << "View: dragEnterEvent";
    10. QGraphicsView::dragEnterEvent(event);
    11. }
    12.  
    13. void myView::dropEvent(QDropEvent *event) {
    14. QPointF pnt;
    15. QPoint pos;
    16.  
    17. qDebug() << "View sees drop";
    18.  
    19. pos = event->pos();
    20. pnt = this->mapToScene( event->pos() );
    21.  
    22. if (itm = itemAt( pos ) )
    23. {
    24. qDebug("There is an item here");
    25. QGraphicsView::dropEvent(event);
    26. return;
    27. }
    28. else
    29. {
    30. qDebug("Blank area");
    31. }
    32. // Is there something underneath?
    33.  
    34. QByteArray ba = event->mimeData()->data("foo/bar");
    35. QDataStream ds(&ba, QIODevice::ReadOnly);
    36. while (!ds.atEnd()) {
    37. QString name;
    38. QString path;
    39. QString monid;
    40. MyNode * nd;
    41. ds >> name >> path >> monid;
    42. qDebug() << name << path << monid;
    43.  
    44. //
    45. // This view is inside the central widget which is in the mainwindow, so node
    46. // parent is the mainwindow so it can access the action functions. a bit
    47. // untiy huh?
    48. //
    49. if (QString("Button")==name)
    50. nd = new MyNode( (QWidget*)(this->parent())->parent(), name, path, monid, MyNode::BUTTON);
    51. else
    52. nd = new MyNode( (QWidget*)(this->parent())->parent(), name, path, monid, MyNode::POINT);
    53. sc = this->scene();
    54.  
    55. pnt = this->mapToScene( event->pos() );
    56. qDebug() << pnt;
    57. nd->setPos(pnt);
    58. sc->addItem(nd);
    59. }
    60. }
    61.  
    62. void myView::dragMoveEvent(QDragMoveEvent *event) {
    63. }
    To copy to clipboard, switch view to plain text mode 


    Scene header

    Qt Code:
    1. class myScene : public QGraphicsScene
    2. {
    3. public:
    4. myScene(QObject *parent);
    5. void dropEvent(QGraphicsSceneDragDropEvent * event);
    6. void dragEnterEvent(QGraphicsSceneDragDropEvent * event);
    7. };
    To copy to clipboard, switch view to plain text mode 

    scene code

    Qt Code:
    1. myScene::myScene(QObject*parent): QGraphicsScene(parent)
    2. {
    3. }
    4.  
    5. void myScene::dropEvent(QGraphicsSceneDragDropEvent * event)
    6. {
    7. qDebug() << "scene: dropEvent";
    8. qDebug() << items(event->scenePos()).count();
    9.  
    10. this->setFocus(Qt::NoFocusReason);
    11.  
    12.  
    13. gi = this->itemAt( event->scenePos() );
    14.  
    15. if (gi)
    16. {
    17. qDebug() << "Scene(dropEvent); there is an item thar";
    18. gi->setFocus(Qt::NoFocusReason);
    19. gi->setSelected(true);
    20. }
    21.  
    22. QGraphicsScene::dropEvent(event);
    23. }
    24.  
    25. void myScene::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
    26. {
    27. event->acceptProposedAction();
    28.  
    29. gi = this->itemAt( event->scenePos() );
    30.  
    31. if (gi)
    32. {
    33. qDebug() << "Scene(dragEnterEvent); there is an item thar";
    34. gi->setFocus(Qt::NoFocusReason);
    35. gi->setSelected(true);
    36. }
    37.  
    38. qDebug() << "scene: dragEnterEvent";
    39. QGraphicsScene::dragEnterEvent(event);
    40. }
    To copy to clipboard, switch view to plain text mode 

    And finally item header

    Qt Code:
    1. class MyNode : public QGraphicsItem
    2. {
    3. public:
    4. int MonId;
    5. int ValInt;
    6. int DataType;
    7. QString endnode;
    8. QString path;
    9. QString monid;
    10.  
    11. enum MimicType { LAMP, BUTTON, POINT };
    12. MimicType mimic;
    13. int EditMode;
    14.  
    15.  
    16. MyNode(QWidget * parent, QString endnode, QString path, QString monid, MimicType mt);
    17. QRectF boundingRect() const;
    18. void paint(QPainter *painter, const QStyleOptionGraphicsItem * option, QWidget *widget);
    19. void mousePressEvent(QGraphicsSceneMouseEvent * event);
    20. void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
    21. void dropEvent(QGraphicsSceneDragDropEvent * event);
    22. void dragEnterEvent(QGraphicsSceneDragDropEvent * event);
    23. void dragMoveEvent(QGraphicsSceneDragDropEvent * event);
    24. void myupdate();
    25. void createActions();
    26. QAction * myAct;
    27. void passDrop(QDropEvent * event);
    28.  
    29. private slots:
    30. void doMyAct();
    31. QWidget * parent;
    32. };
    To copy to clipboard, switch view to plain text mode 

    and the item drag methods...

    Qt Code:
    1. void MyNode::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
    2. {
    3. qDebug() << "Node: dragEnterEvent";
    4. //event->setAccepted(true);
    5. //event->accept();
    6. event->acceptProposedAction();
    7. QGraphicsItem::dragEnterEvent(event);
    8. }
    9. void MyNode::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
    10. {
    11. qDebug() << "Node: dragMoveEvent";
    12. event->accept();
    13. QGraphicsItem::dragMoveEvent(event);
    14. }
    15. void MyNode::dropEvent(QGraphicsSceneDragDropEvent * event)
    16. {
    17. qDebug() << "Node: dropEvent";
    18. QGraphicsItem::dropEvent(event);
    19. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    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: QGraphicsItem .. Scene .. View question?

    What is exactly the point of intercepting those events in both the view and the scene? Do you have more than one view on the same scene and want them to behave differently?
    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.


  13. #13
    Join Date
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsItem .. Scene .. View question?

    Hi again,

    I'll want to capture events in the view to resize the view, zoom, pan, etc.

    In the scene I want to have items, which will allow drops and also
    have context menu(edit mode). Then in (run mode) the items will have
    mouse click events which result in some internal processing.

    (These drops configure the items with various fields in a database,
    then these items change colour or text when the values in the
    database change)

    Eventually, more complex behaviour, linking from one item to another,
    moving items around, etc.

    thanks again,

    DB

  14. #14
    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: QGraphicsItem .. Scene .. View question?

    But I'm asking about drop events specifically. I don't see the point in intercepting them in both the view and the scene unless you have more than one view on the same scene. In general in 90% of the cases it is enough to intercept any events just in the scene and items themselves.
    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.


  15. #15
    Join Date
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsItem .. Scene .. View question?

    OK, I'll try without catching these events in the view. So now I've removed
    myview and am back to using the base QGraphicsView with view->setAcceptDrops(true);

    However, when I drag into the view/scene pane I get the 'no entry' cursor
    and cannot drop into the view/scene.

    Any ideas?

    DB

  16. #16
    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: QGraphicsItem .. Scene .. View question?

    You need to reimplement dragEnterEvent on the scene and possibly dragMoveEvent as well. The same needs to be done for item classes you want to accept drops.
    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.


  17. #17
    Join Date
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsItem .. Scene .. View question?

    Hi,

    Yes, I have dragMoveEvent, dragEnterEvent and dropEvent defined in
    the scene.

    I also have setAcceptDrops(true) for the scene.

    However, it still has the 'no entry' cursor when trying a drag-drop into
    this view/scene.

    DB

  18. #18
    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: QGraphicsItem .. Scene .. View question?

    Well, being defined and being defined properly are two different things. For instance if you call the base class implementation of dragEnterEvent() it will probably reject the event despite that you accepted it in your own code.
    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.


  19. #19
    Join Date
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QGraphicsItem .. Scene .. View question?

    Well, being defined and being defined properly are two different things. For instance if you call the base class implementation of dragEnterEvent() it will probably reject the event despite that you accepted it in your own code.
    Oooh, now that's a nice cryptic response. :-)

    So I removed the 'QGraphicsScene::dragMoveEvent(event)' from my dragMoveEvent method and I can see the drop in the scene.

    However, when there is an item underneath the cursor I still dont see the event in
    my item dropEvent()?

    Is there documentation which details when to call, and when not to call the base methods of the QGraphicsScene/QGraphicsItem?

  20. #20
    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: QGraphicsItem .. Scene .. View question?

    Quote Originally Posted by DirtyBrush View Post
    However, when there is an item underneath the cursor I still dont see the event in
    my item dropEvent()?
    Are you calling the base class implementation of the item's dragEnterEvent() and/or dragMoveEvent()?

    Is there documentation which details when to call, and when not to call the base methods of the QGraphicsScene/QGraphicsItem?
    There is a simple rule of a thumb - if you want to replace what the base class does with your own logic, don't call the base class implementation. If you want to extend what the base class does, call the base class implementation. You can't accept and ignore the event at the same time so calling the base class implementation in case of handling drops makes sense only if you want to say "I don't want to make a decision about the drag, I'm leaving it up to Qt".
    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.


Similar Threads

  1. Scene vs. multiple views
    By lni in forum Qt Programming
    Replies: 16
    Last Post: 19th July 2022, 01:47
  2. Replies: 6
    Last Post: 8th June 2009, 21:44
  3. Model / View - Design Question
    By antarctic in forum Qt Programming
    Replies: 8
    Last Post: 8th June 2009, 07:39
  4. View, Scene, Item and thread??
    By dungsivn in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2008, 19:21
  5. Deleting a scene from QGraphicsItem mouseEvent
    By JonathanForQT4 in forum Qt Programming
    Replies: 5
    Last Post: 10th April 2007, 11:27

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.