Results 1 to 20 of 22

Thread: QGraphicsItem .. Scene .. View question?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  2. #2
    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  4. #4
    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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  6. #6
    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 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  8. #8
    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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  10. #10
    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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  12. #12
    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

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
  •  
Qt is a trademark of The Qt Company.