Results 1 to 15 of 15

Thread: Drag and drop files

  1. #1
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drag and drop files

    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.

  2. #2
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Drag and drop files

    For files, you can use "text/uri-list" and check if every url is local file.

  3. #3
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and drop files

    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.

  4. #4
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and drop files

    I checked MIME type like this:
    Qt Code:
    1. QString mimeData_2_fileName( const QMimeData * mimeData )
    2. {
    3. if ( mimeData->hasUrls() )
    4. {
    5. foreach ( const QUrl & url, mimeData->urls() )
    6. {
    7. QString str = url.toLocalFile();
    8. if ( str.isEmpty() == false )
    9. {
    10. if ( QFileInfo( str ).suffix() == "ini" ) // this is for my own purposes only. you can remove this
    11. {
    12. return str;
    13. }
    14. }
    15. }
    16. }
    17. return QString();
    18. }
    19.  
    20. void Widget::dragEnterEvent( QDragEnterEvent * event )
    21. {
    22. if ( mimeData_2_fileName( event->mimeData() ).isEmpty() == false )
    23. {
    24. event->acceptProposedAction();
    25. }
    26. }
    27.  
    28. void Widget::dropEvent( QDropEvent * event )
    29. {
    30. QString fileName = mimeData_2_fileName( event->mimeData() );
    31. if ( fileName.isEmpty() == false )
    32. {
    33. openFile( fileName ); // or do that you want with it
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to borisbn for this useful post:

    MTK358 (9th September 2010)

  6. #5
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and drop files

    How to I enable dropping to (but not dragging from) a custom model (and when the view is created in Designer)?

  7. #6
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and drop files

    Anyone?

    some extra characters to satisfy 10 char limit

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and drop files

    Quote Originally Posted by MTK358 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and drop files

    But I did that and it doesn't work! Here are the flags() and dropMimeData() implementations:

    Qt Code:
    1. qint32 TagTreeModel::getTagId(const QModelIndex &index)
    2. {
    3. return index.internalId();
    4. }
    5.  
    6. bool TagTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
    7. {
    8. qDebug() << "Data dropped.";
    9. return true;
    10. }
    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?


  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and drop files

    I don't see the flags() implementation for your model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and drop files

    Quote Originally Posted by wysota View Post
    I don't see the flags() implementation for your model.
    I accidentally posted another method. Here's flags():

    Qt Code:
    1. Qt::ItemFlags TagTreeModel::flags(const QModelIndex &index) const
    2. {
    3. if (!index.isValid())
    4. return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
    5. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDropEnabled;
    6. }
    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.

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and drop files

    Hmm... I think you also have to reimplement QAbstractItemModel::mimeTypes() to tell the view what mime-types you expect to accept.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and drop files

    It said in your link:

    Quote Originally Posted by QAbstractItemModel::mimeTypes() Documentation
    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.

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and drop files

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following user says thank you to wysota for this useful post:

    MTK358 (9th September 2010)

  16. #14
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and drop files

    It works now. I even changed the dropMimeData() function to print the path to each file, and it's all working as it should.

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and drop files

    Quote Originally Posted by MTK358 View Post
    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
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 10th June 2010, 16:13
  2. Replies: 0
    Last Post: 4th May 2010, 11:24
  3. Drag and Drop
    By cwnelatury in forum Newbie
    Replies: 3
    Last Post: 29th April 2009, 01:51
  4. Drag and drop
    By iswm in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2007, 02:41
  5. Drag and Drop (drop example)
    By din9 in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 19:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.