Drag and Drop of QListView Items

New Moon picture New Moon · Oct 16, 2012 · Viewed 8.1k times · Source

Please help me out to solve this.... I have a QListView on the Left side and a QWidget on the other side. In QListView i had added few items using QStandardItem. Now i want to Drag and Drop the QListView Items to the other side QWidget and i have to do the same with the QWidget also. I can drag and drop my QListView Items inside the QListView itself by using

listView.setAcceptDrops(true);
listView.setDragEnabled(true);
listView.setDragDropMode(QAbstractItemView::InternalMove); 

this is working fine inside the QListView alone. I want to Drag and Drop the QListView Items to the other Widget. How can i do this? I know that i have to handle the Events like

void dropEvent(QDropEvent *);
void dragMoveEvent(QDragMoveEvent *);
void dragEnterEvent(QDragEnterEvent *);
void mousePressEvent(QMouseEvent *);

i just tried it like this

void Example::dragMoveEvent(QDragMoveEvent *e)
{
    // The event needs to be accepted here
    e->accept();
}

void Example::dragEnterEvent(QDragEnterEvent *e)
{
    // Set the drop action to be the proposed action.
    e->acceptProposedAction();
}

void Example::dropEvent(QDropEvent *e)
{
    qDebug("Items Dropped");
}

As i just tried with some qDebug() , this is Working when i drag an Item from my QListView and Drop it in the QWidget and am getting the output as "Items Dropped". But i dont know how to bring my exact QListView's Items here.

Answer

quinmars picture quinmars · Oct 20, 2012

You don't have to subclass the view. All the drag and drop stuff is handled by the model, hence you have to subclass your standard model. You may want to take a look on the model subclassing reference. Of course you also have to change the drag-and-drop mode of your view to QAbstractItemView::DragDrop to get external drag and drop.