Results 1 to 6 of 6

Thread: Advice catching dropEvent

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

    Default Advice catching dropEvent

    Hello All,

    I have a QGraphicsItem in a QGraphicsScene shown in a QGraphicsView.

    Sometimes I want to catch drops with nothing underneath.

    Sometimes I want to catch drops within items under the mouse.

    Sometimes I want to catch drops within the children items of items under the mouse.

    I created my own dropEvent() method in the view, using QDropEvent with this I
    can add a new item to the scene.

    However, I've got a dropEvent() in my item which uses QGraphicsSceneDragDropEvent
    so I cant just call the item's dropEvent method from the view method. I get a type
    mis match error.

    So far, my hacky work round, is to make my own method myDropEvent in the item
    and call this from the view. I'm sure there is a neater way to do this, any
    advice?

    thanks!

    DB

  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: Advice catching dropEvent

    If you need any special tricks then it means you did something wrong. You should be able to catch the event in the item, the scene or the view just like that. Remember to call the base class implementation of the event if you want it to continue propagation - maybe that's what you are missing.
    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
    Jul 2009
    Posts
    22
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Advice catching dropEvent

    Thanks for the reply.

    I'm a bit new to C++, base class implementation... um is that the single
    colon thing after the method name? :-)

    So when I have -

    void myView::dropEvent(QDropEvent *event)
    { ...

    I should have -

    void myView::dropEvent(QDropEvent *event):dropEvent(event)
    { ...

    So this will pass all events onto the scene and then the items? I was a bit
    confused with the wording of ignore() and accept() calls for an event.

    If I call
    ignore() of the event, will it propogate down to the items?

    Also, why would I call accept()? If I'm already in the view handler then I'm
    already processing it, and by implication accepted?

    Thanks again for your response,

    DB

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

    Default Re: Advice catching dropEvent

    Hello again,

    Some more info...

    The ':basemethod(param)' is only for constructors.

    I tried

    QGraphicsView::dropEvent(event);

    In my view code, but didn't get this in the item.

    Tried setting the item focus before this as one bit of documentation said
    if the item was not in focus event would be ignored.

    Tried various combinations of ignore() and accept(), still nothing passed
    through to the item.

    Do I need to create my own scene class which captures the dropEvent too?
    And use my scene dropEvent to pass the event onto the item in the scene?

    DB

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

    Default Re: Advice catching dropEvent

    So a different approach, I've removed the DnD overloading from
    my version of the QGraphicsView and created my own QGraphicsScene.
    I see the dragEnterEvent but not the dropEvent.

    Pleeeeeeease, any ideas where I'm going wrong?

    myScene::myScene(QObject*parent): QGraphicsScene(parent)
    {

    }

    void myScene::dropEvent(QGraphicsSceneDragDropEvent * event)
    {
    qDebug() << "Scene sees drop";
    }

    void myScene::dragEnterEvent(QGraphicsSceneDragDropEven t * event)
    {
    //event->setAccepted(true);
    event->acceptProposedAction();
    qDebug() << "scene drag enter";
    QGraphicsScene::dragEnterEvent(event);
    }

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

    Default Re: Advice catching dropEvent

    Hello Again,

    So with a subclass of QGraphicsView and of QGraphicsScene and of QGraphicsItem
    I can use the view to create a new item when there is nothing under the mouse
    in the scene. Then when there is an item, the scene now gets the dropEvent
    and the scene can see that there is an item under the mouse.

    However, the item doesn't get the event. Do I need to explicitly tell the scene to
    give the event to the item, or some other magic?

    void myScene::dropEvent(QGraphicsSceneDragDropEvent * event)
    {
    qDebug() << "Scene sees drop";
    //event->ignore();
    //event->accept();
    //event->acceptProposedAction();

    QGraphicsItem *gi;

    gi = this->itemAt( event->scenePos() );

    if (gi)
    {
    qDebug() << "Scene; there is an item thar";
    // but item never gets the event :-(
    gi->setFocus(Qt::NoFocusReason);
    gi->setSelected(true);
    //gi->setAcceptDrops(true);
    }

    QGraphicsScene::dropEvent(event);
    }

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.