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

Thread: Drag and Drop, dropEvent not being called?

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

    Default Drag and Drop, dropEvent not being called?

    Hi,

    I have a tree widget which allows items to be dragged. I have a derived tableview which accepts drops, I have reimplemented the dragEnterEvent and dropEvent in here, the dragEnterEvent gets called but the dropEvent doesn't. At the moment I am just accepting any types of drops.

    Code in the tableview :

    Qt Code:
    1. void DATableView::dropEvent(QDropEvent *e)
    2. {
    3. QString strText = e->mimeData()->text();
    4. e->acceptProposedAction();
    5. }
    6.  
    7. void DATableView::dragEnterEvent(QDragEnterEvent* e)
    8. {
    9. e->accept();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Any idea what I'm missing?

    Regards,
    Steve

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    You have to handle this in the model, with dropMimeData.

    Regards

  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,

    Got this working now, I needed to also reimplement the dragMoveEvent. Just now need to figure out how to put the dragged item into my table model...

    Regards,
    Steve

  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?

    Thanks,

    My tree consists of objects of type CSignal ( I use set data on the items for this ). When you drag an item from the tree ( CSignal ) I want to get this data as type CSignal, how do I do this? Is this a custom mime type?

    Regards,
    Steve

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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?

    Might I ask why do you reimplement methods from the view to handle this?

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

    I have done this as I've been following an example which does this? Is there a better way?!

    I'm now trying to subclass QMimeData in order to have my own custom data.

    Regards,
    Steve

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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?

    Which example does that? I'm pretty sure all drag and drop examples that concern views reimplement mimeTypes(), mimeData() and dropMimeData() from the model.

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

    Well the example ( C++ GUI Programming with Qt4) doesn't use a model so I guess this is why it handles the events in the view/widget. I have just implemented the dropMimeData method in my model but it doesn't get called, I guess this is because I have the events implemented in my view?!

    Kind regards,
    Steve

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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
    Well the example ( C++ GUI Programming with Qt4) doesn't use a model so I guess this is why it handles the events in the view/widget.
    Yes, it doesn't have the required infrastructure which is already present in views.
    I have just implemented the dropMimeData method in my model but it doesn't get called, I guess this is because I have the events implemented in my view?!
    Correct. You only need to reimplement the three methods mentioned, even if you want to carry custom data.

  10. The following user says thank you to wysota for this useful post:

    steg90 (21st May 2007)

  11. #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?

    Thanks,

    I have removed the drag/drop events from my view and implemented the three methods you mentioned into my model, but they don't get called? Do you have to set up the model to accept drops or something?

    Regards,
    Steve

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

  13. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 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

  14. #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?

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

  15. #14
    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

  16. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 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

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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.

  18. #17
    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

  19. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 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.

  20. #19
    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

  21. #20
    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

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.