What MIME type and format to file managers use when dragging and dropping files? I would like a custom tree model in my application to accept file drops.
What MIME type and format to file managers use when dragging and dropping files? I would like a custom tree model in my application to accept file drops.
For files, you can use "text/uri-list" and check if every url is local file.
Is that how all file managers do it? For example I can drag a file from Thunar (a GTK+ file manager) into Dolphin (a KDE/Qt file manager) and it works.
I checked MIME type like this:
Qt Code:
{ if ( mimeData->hasUrls() ) { { if ( str.isEmpty() == false ) { { return str; } } } } } { if ( mimeData_2_fileName( event->mimeData() ).isEmpty() == false ) { event->acceptProposedAction(); } } { if ( fileName.isEmpty() == false ) { openFile( fileName ); // or do that you want with it } }To copy to clipboard, switch view to plain text mode
MTK358 (9th September 2010)
How to I enable dropping to (but not dragging from) a custom model (and when the view is created in Designer)?
Anyone?
some extra characters to satisfy 10 char limit
You return ItemIsDropEnabled from QAbstractItemModel::flags() for each index to accept drops and for an invalid index if you want to accept drops on the "empty" view. You also have to reimplement QAbstractItemModel::dropMimeData() to actually handle the drop.
But I did that and it doesn't work! Here are the flags() and dropMimeData() implementations:
Qt Code:
{ return index.internalId(); } bool TagTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { qDebug() << "Data dropped."; return true; }To copy to clipboard, switch view to plain text mode
When I drag anything onto the view, it's rejected (the icon "flies back" to its original location) and the application doesn't print "Data dropped.". Note that the view is created in Designer (screenshot below). How do I set it up to work?
I accidentally posted another method. Here's flags():
Qt Code:
{ if (!index.isValid()) return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled; return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDropEnabled; }To copy to clipboard, switch view to plain text mode
Note that I only want to accept drops onto this model, not drag from this model.
Hmm... I think you also have to reimplement QAbstractItemModel::mimeTypes() to tell the view what mime-types you expect to accept.
It said in your link:
What does that mean? The data I want to accept (local file URLs) is unrelated to the type of the items in my model.Originally Posted by QAbstractItemModel::mimeTypes() Documentation
It's ok, just pass it the list of mime-types you want to receive (text/uri-list). Since you are not dragging any items from the model, you don't need to worry about it.
MTK358 (9th September 2010)
It works now. I even changed the dropMimeData() function to print the path to each file, and it's all working as it should.
Bookmarks