Results 1 to 20 of 37

Thread: Drag and Drop, dropEvent not being called?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    got it - setSupportedDragActions !

    mimeTypes gets called but dropMimeData and mimeData are not...

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drag and Drop, dropEvent not being called?

    Do you return drag and drop flags in flags()?
    J-P Nurmi

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Hi JPN,

    I have the following flags function in my model code :

    Qt Code:
    1. Qt::ItemFlags DACanTreeModel::flags(const QModelIndex &index) const
    2. {
    3. Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
    4. if (index.isValid())
    5. return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
    6. else
    7. return Qt::ItemIsDropEnabled | defaultFlags;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Still can't get the dropMimeData function to be called...

  4. #4
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Also, the flags for the item in the treewidget I'm dragging are set to ItemIsDragEnabled. Can't understand why dropMimeData isn't being called...

    Regards,
    Steve

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,371
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Drop, dropEvent not being called?

    Also if you drag between two widgets, make sure they understand each other's mime-types.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drag and Drop, dropEvent not being called?

    Presumably you are already following the instructions in Using Drag and Drop with Item Views, because your flags() looks so similar. But does your view still have overridden drag/dragmove/drop event handlers? If so, I'd suggest commenting them out. For example accepting the event in dropEvent() and then calling the base class implementation makes it never drop..
    J-P Nurmi

  7. #7
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Hi,

    Yes, following that example, I had removed the events from my view. Still not being called, got to be something simple?!

    This is the code I have in my treewidget ( items in here I want to be draggable ) :

    Qt Code:
    1. void DASignalTree::mousePressEvent ( QMouseEvent * event )
    2. {
    3. if( event->button() == Qt::LeftButton )
    4. {
    5. m_startPos = event->pos();
    6. }
    7. QTreeWidget::mousePressEvent( event );
    8. }
    9.  
    10. void DASignalTree::mouseMoveEvent( QMouseEvent * event )
    11. {
    12. if( event->buttons() & Qt::LeftButton )
    13. {
    14. int distance = ( event->pos() - m_startPos).manhattanLength();
    15. if( distance >= QApplication::startDragDistance() )
    16. {
    17. QTreeWidgetItem *item = itemAt( m_startPos );
    18. if ( item )
    19. {
    20. startDrag();
    21. }
    22. }
    23. }
    24. // QTreeWidget::mouseMoveEvent( event );
    25. }
    26.  
    27. void DASignalTree::startDrag()
    28. {
    29. QTreeWidgetItem* dragItem = currentItem();
    30. DAMimeData* pMimeData = new DAMimeData;
    31. pMimeData->setText( dragItem->text( 0 ) );
    32. QDrag* pDrag = new QDrag( this );
    33. pDrag->setMimeData( pMimeData );
    34. pDrag->setPixmap( QPixmap("images/new.png") );
    35.  
    36. theApp->AddText( dragItem->text( 0 ) );
    37.  
    38. if( pDrag->start(Qt::MoveAction) == Qt::MoveAction)
    39. delete dragItem;
    40. }
    To copy to clipboard, switch view to plain text mode 

    and the code in my model :

    Qt Code:
    1. bool DACanTreeModel::dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent )
    2. {
    3. return true;
    4. }
    5.  
    6. QStringList DACanTreeModel::mimeTypes () const
    7. {
    8. return QStringList();
    9. }
    10.  
    11. QMimeData * DACanTreeModel::mimeData ( const QModelIndexList & indexes ) const
    12. {
    13. return new QMimeData();
    14. }
    15.  
    16.  
    17. Qt::DropActions DACanTreeModel::supportedDropActions() const
    18. {
    19. return Qt::CopyAction | Qt::MoveAction;
    20. }
    21.  
    22. Qt::ItemFlags DACanTreeModel::flags(const QModelIndex &index) const
    23. {
    24. Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
    25. if (index.isValid())
    26. return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
    27. else
    28. return Qt::ItemIsDropEnabled | defaultFlags;
    29. }
    To copy to clipboard, switch view to plain text mode 

    And finally the view constructor :

    Qt Code:
    1. DATableView::DATableView(QWidget *parent)
    2. : QTableView(parent)
    3. {
    4. setAcceptDrops(true);
    5. setDropIndicatorShown(true);
    6. }
    To copy to clipboard, switch view to plain text mode 

    I'm at a loss?!

    Regards,
    Steve

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,371
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Drop, dropEvent not being called?

    The tree widget also has the before mentioned methods which you should reimplement.

  9. #9
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Thanks,

    So I need to also implement dropMimeData, mimeData and mimeTypes in my tree widget? I guess I can ignore the dropMimeData as my tree won't allow any drop events?

    Regards,
    Steve

  10. #10
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Implemented the methods mimeData, dropmimeData and mimeTypes in the tree widget, but still no dropMimeData being called...

    When I was doing the drag/drop in the view, it seemed a lot easier and seemed to work, just can't understand what is wrong.

    Regards,
    Steve

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,371
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Drop, dropEvent not being called?

    Here is a working example.
    Attached Files Attached Files

  12. #12
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Many thanks, that is very kind of you.

  13. #13
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop, dropEvent not being called?

    Many thanks for the example, I think way back in the thread or a related one you did mention that you should only implement the dropMimeData, mimeData and mimeTypes functions, I should have listened!!! I had implemented a few mouse event functions and a few drag events in my tree widget which I shouldn't have done, this was stopping it working.

    Is it possible to pass over the object associated with the item being dragged from the tree widget?

    Kind regards,
    Steve

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,371
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Drop, dropEvent not being called?

    Quote Originally Posted by steg90 View Post
    Many thanks for the example, I think way back in the thread or a related one you did mention that you should only implement the dropMimeData, mimeData and mimeTypes functions, I should have listened!!! I had implemented a few mouse event functions and a few drag events in my tree widget which I shouldn't have done, this was stopping it working.
    I think we told you a few times to remove the mouse event handlers

    Is it possible to pass over the object associated with the item being dragged from the tree widget?
    In general, yes. You can pass a pointer or something like that, you can subclass QMimeData if you want. But the proper way to go would be to serialize your object to QByteArray, transfer that array using QMimeData::setData and reconstruct the object on the other side.

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.