Could you prepare a minimal compilable example reproducing the problem? I've used MoveActions with models and they worked just fine - dropMimeData was called.
Could you prepare a minimal compilable example reproducing the problem? I've used MoveActions with models and they worked just fine - dropMimeData was called.
Hi,
sorry it took so long. Here is my (very simple) tree widget
----------------------------------------------------------------------------
-----------------------------------------------------------------------------Qt Code:
#include "myQTTreeWidget.h" #include <QDragEnterEvent> #include <QDragMoveEvent> #include <QList> { setAcceptDrops(true); setDragEnabled(true); setHeaderLabel("Name"); setColumnCount(1); item1->setText(0,"hello"); item2->setText(0,"world!"); item3->setText(0,"whats"); item4->setText(0,"up?"); } myQTTreeWidget::~myQTTreeWidget() { } { e->accept(); } { e->accept(); } { } bool myQTTreeWidget::dropMimeData ( QTreeWidgetItem * newParentPtr, int index, const QMimeData * data, Qt::DropAction action ) { return true; } Qt::DropActions myQTTreeWidget::supportedDropActions () const { return Qt::CopyAction|Qt::MoveAction; }To copy to clipboard, switch view to plain text mode
If you want to reproduce the behaviour, you can place a breakpoint into dropMimeData (or some kind of fprintf(...) output). You will see that this function is called if you try to drag & drop one of the items (without pressing Shift!). But it is not called if you press the Shift key while you're dragging/dropping the item. If you try to drag&drop an item without pressing Shift - nothing happens after dropping the item (that's ok, the drop function is empty). If you drag&drop an item while the Shift key is pressed, then the item is moved after dropping it (wherever this behaviour is implemented).
Maybe there is something totally wrong with my code. Maybe I made something very stupid? Any suggestion is very welcome.
Thanks, take care
I added the following lines:
Qt Code:
{ fprintf(stderr,"dropped\n"); }To copy to clipboard, switch view to plain text mode
The dropEvent is called but it does not reach the dropMimeData funtion. I guess I have to build some qt debug libs...
But still: Any suggestion is very wellcome.
QAbstractItemModel::dropMimeData() is called by QAbstractItemView::dropEvent() so reimplementing dropEvent() without calling the base class implementation prevents dropMimeData() from being called. Furthermore, reimplementing other drag and drop related event handlers, again, without calling the base class implementation like in the code above, prevents QAbstractItemView DND from functioning properly. Take a look at src/gui/itemviews/qabstractitemview.cpp and see what QAbstractItemView::dragEnterEvent() and QAbstractItemView::dragMoveEvent() do. Writing:
overrides all that functionality with.. nothing.Qt Code:
{ e->accept(); } { e->accept(); }To copy to clipboard, switch view to plain text mode![]()
J-P Nurmi
Greetings ,
In the two functions below override the original events by accepting the event but what next.
It will accept the event and no action is performed. ( Try emitting a signal within these functions and perform the required actions accordingly )
void myQTTreeWidget::dragEnterEvent(QDragEnterEvent *e)
void myQTTreeWidget::dragMoveEvent(QDragMoveEvent *e)
Regards
-Ram
Is it a continuation of the same problem by a different forum user, a completely separate issue or an answer to the original problem? I got completely lost here...![]()
Thank you very much for your help.
Even if it's a very silly question, I have to ask it (sorry 'bout that):
Whats the "correct" way to implement the dragEnterEvent and dragMoveEvent functions - is this the way?
Qt Code:
{ // always call base class implementation if ( isDragEnterPossible(e) ) { e->accept(); } else { e->ignore(); } } { // always call base class implementation if ( isDragMovePossible(e) ) { e->accept(); } else { e->ignore(); } }To copy to clipboard, switch view to plain text mode
isDragMovePossible and isDragEnterPossible are member functions, returning a bool value and telling us if dropping is possible.
The question is why would you want to reimplement them? What's wrong with the default implementation?
Thank you for your answer.
So "what is this all about":
I have an application with lots of different widgets (treeviews, listviews, iconviews .... ). All widgest support dnd (all of them are still old qt3 style, but will/should () be "converted" soon). In my (derived) QTreeView I want do accept the drags from some of the other widgets, but not from all of the other widget. I thought the dragEnterEvent can (has to) be used for defining which events are accepted and which are not:
Qt Code:
{ if ( e ) { if ( e->provides("mystuff/mything") ) { e->accept(); } else { e->ignore(); } } }To copy to clipboard, switch view to plain text mode
I also have different items (all derived from QTreeWidgetItem of course) in my treeview. Some of them accept drops and some of them don't. So I use the dragMoveEvent to get the "current dropping target" by calling
Qt Code:
{ bool accept = false; if ( item ) { if ( dynamic_cast<MySpecialListViewItem*>(item) ) { accept = true; } } if ( accept ) { e->accept(); } else { e->ignore(); } }To copy to clipboard, switch view to plain text mode
and check (by dynamic cast) if this (derived) QTreeWidgetItem is one of the items that accept drops.
I use the event->accept() or event->ignore() function to indicate if dropping is possible or not.
ps. Sorry that this has become a beginner tutorial on using drag and drop with QTreeViews, maybe I was running completely in the wrong direction?
But most of what you want is already implemented by default handlers, there is no need to reinvent the wheel. You can tell the widget which types of data it should accept. Take a look at mimeTypes(), mimeData() and dropMimeData().
Bookmarks