PDA

View Full Version : Drag and drop files



MTK358
8th September 2010, 14:21
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.

Ben1
8th September 2010, 14:41
For files, you can use "text/uri-list" and check if every url is local file.

MTK358
8th September 2010, 14:53
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.

borisbn
8th September 2010, 15:46
I checked MIME type like this:


QString mimeData_2_fileName( const QMimeData * mimeData )
{
if ( mimeData->hasUrls() )
{
foreach ( const QUrl & url, mimeData->urls() )
{
QString str = url.toLocalFile();
if ( str.isEmpty() == false )
{
if ( QFileInfo( str ).suffix() == "ini" ) // this is for my own purposes only. you can remove this
{
return str;
}
}
}
}
return QString();
}

void Widget::dragEnterEvent( QDragEnterEvent * event )
{
if ( mimeData_2_fileName( event->mimeData() ).isEmpty() == false )
{
event->acceptProposedAction();
}
}

void Widget::dropEvent( QDropEvent * event )
{
QString fileName = mimeData_2_fileName( event->mimeData() );
if ( fileName.isEmpty() == false )
{
openFile( fileName ); // or do that you want with it
}
}

MTK358
8th September 2010, 19:26
How to I enable dropping to (but not dragging from) a custom model (and when the view is created in Designer)?

MTK358
9th September 2010, 19:24
Anyone?

some extra characters to satisfy 10 char limit

wysota
9th September 2010, 20:09
How to I enable dropping to (but not dragging from) a custom model (and when the view is created in Designer)?
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.

MTK358
9th September 2010, 20:44
But I did that and it doesn't work! Here are the flags() and dropMimeData() implementations:

qint32 TagTreeModel::getTagId(const QModelIndex &index)
{
return index.internalId();
}

bool TagTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
qDebug() << "Data dropped.";
return true;
}

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?

http://a.imageshack.us/img72/7108/qtdesignertreeviewprope.png

wysota
9th September 2010, 21:00
I don't see the flags() implementation for your model.

MTK358
9th September 2010, 21:14
I don't see the flags() implementation for your model.

I accidentally posted another method. Here's flags():

Qt::ItemFlags TagTreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDropEnabled;
}

Note that I only want to accept drops onto this model, not drag from this model.

wysota
9th September 2010, 21:53
Hmm... I think you also have to reimplement QAbstractItemModel::mimeTypes() to tell the view what mime-types you expect to accept.

MTK358
9th September 2010, 21:56
It said in your link:


Returns a list of MIME types that can be used to describe a list of model indexes.

What does that mean? The data I want to accept (local file URLs) is unrelated to the type of the items in my model.

wysota
9th September 2010, 22:01
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, 22:08
It works now. I even changed the dropMimeData() function to print the path to each file, and it's all working as it should.

wysota
9th September 2010, 22:27
It works now. I even changed the dropMimeData() function to print the path to each file, and it's all working as it should.

Congratulations :cool: