Results 1 to 3 of 3

Thread: Drag and Drop from One QGraphicsScene to another QGraphicsScene

  1. #1
    Join Date
    May 2017
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Drag and Drop from One QGraphicsScene to another QGraphicsScene

    I have two different custom QGraphicsScene presented by means of two different QGraphicsView. I want to drag and drop items from one scene to another. Therefore I have implemented following in my QGraphicsScene:

    void mousePressEvent
    void mouseMoveEvent
    void dragEnterEvent
    void dragMoveEvent
    void dragLeaveEvent
    void dropEvent


    I want to reject drop; if it is same Scene I am dragging from and dropping into. For this; I need to get QGraphicsSceneDragDropEvent::event->source() and cast it to a proper widget and see if it has originated from the same object by comparing it with 'this' pointer.


    void my_Chart::dragEnterEvent(QGraphicsSceneDragDropEve nt *event)
    {
    my_Chart *chart= qobject_cast<my_Chart *> (event->source());
    if(chart != 0)
    {
    if(chart != this)
    // do something relevent
    else
    //reject drop
    }
    else
    {
    //reject drop
    }

    }

    The casting is always failing, whatever I do. Chart is always 0. I have checked documentation, It says that:http://doc.qt.io/qt-5/qgraphicsscene...nt.html#source
    This function returns the QGraphicsView that created the QGraphicsSceneDragDropEvent. So I tried:

    my_ChartView *chart= qobject_cast<my_ChartView *> (event->source());

    But this also failed. Kindly help.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,719
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Drag and Drop from One QGraphicsScene to another QGraphicsScene

    Create a temporary QObject * variable in your event and assign event->source() to it. Set a breakpoint in the debugger after that assignment and examine the QObject pointer to see what it actually points to. Alternatively, insert a qDebug() statement to print the value of event->source() (which will also tell you what it points 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.

  3. #3
    Join Date
    May 2017
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Drag and Drop from One QGraphicsScene to another QGraphicsScene

    Thank you.

Similar Threads

  1. Drag and drop Items in QGraphicsScene
    By MinMin in forum Newbie
    Replies: 4
    Last Post: 27th March 2014, 14:31
  2. Drag&Drop using QGraphicsScene
    By topcraft in forum Qt Programming
    Replies: 0
    Last Post: 24th October 2011, 14:46
  3. Drag & Drop and Context Menu problem QGraphicsScene
    By onurozcelik in forum Qt Programming
    Replies: 0
    Last Post: 19th August 2010, 09:12
  4. drag&drop from QGraphicsScene to QListWidget
    By hakiim35 in forum Qt Programming
    Replies: 4
    Last Post: 7th July 2010, 12:29
  5. Drag and Drop in QGraphicsScene misses items
    By enno in forum Qt Programming
    Replies: 2
    Last Post: 30th August 2009, 16:17

Tags for this Thread

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.