
Originally Posted by
pir
ok... stuck again.
I have problem with passing the information that is dragged... I've tried QAbstractModel::mimeData(), do I need to reimplement it or is it just ok to pass the QModelIndex into it?
Then I have a big problem with retrieving the QModelIndex back from the QMimeData.... how am I supposed to do that? There is a function called QMimeData::retrieveData(), can I use that one as it is or do I reimplement it? It is protected, which I find kind of weird if it supposed to be used in this way...
You should not pass indexes there. You need to encode your data as some custom mime-type (for example application/x-my-mime-type). If you only drag within the same widget, you can for example use the index row number as the encoded form of data (as long as you're able to recreate an item from it). The easiest way is something like this:
QMimeData * MyModel
::mimeData ( const QModelIndexList
& indexes
) const{ rows
+= QString::number(index.
row())+"; ";
}
mime->setText(rows);
return mime;
}
QMimeData * MyModel::mimeData ( const QModelIndexList & indexes ) const{
QMimeData *mime = new QMimeData;
QString rows;
foreach(QModelIndex index, indexes){
rows += QString::number(index.row())+"; ";
}
mime->setText(rows);
return mime;
}
To copy to clipboard, switch view to plain text mode
In dropMimeData just reconstruct indices from their row values and you're done. Of course you may choose to do it more properly and use QByteArray and a custom mime-type instead of text/plain (which is used when using setText()). Or you could even do it even more properly and encode not the index of the item but its data (as should be done with "real" drag&drop).
Bookmarks