Results 1 to 3 of 3

Thread: drag and drop: drag item from QListWidget to desktop or any open directory

  1. #1
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default drag and drop: drag item from QListWidget to desktop or any open directory

    Hi everyone!
    I have QListWidget who contains list of files path. This files i'm dropped from explorer,
    but i don't know how to implement drag and drop from QListWidget to the desktop or explorer.
    This is my try...
    Qt Code:
    1. void FileListWidget::startDrag()
    2. {
    3. if (currentItem()) {
    4. QByteArray data;
    5. QFile src(currentItem()->text());
    6. src.open(QIODevice::ReadOnly);
    7. data = src.readAll();
    8. src.close();
    9. QMimeData *mime = new QMimeData;
    10. mime->setData("text/uri-list", data);
    11.  
    12. QDrag *drag = new QDrag(this);
    13. drag->setMimeData(mime);
    14. drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    ... but it's not working (error when copying file). Please explain me, what should I do to solve this problem?

    P.S. Sorry for my bad English
    Last edited by Vovasty; 29th January 2014 at 14:55.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop: drag item from QListWidget to desktop or any open directory

    Ah, only a minor misunderstanding.

    You don't have to read the file and send its contents, you just send its filename.

    Something like this

    Qt Code:
    1. QFIleInfo fileInfo(currentItem()->text());
    2. QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
    3.  
    4. QMimeData *mime = new QMimeData;
    5. mime->setUrls(QList<QUrl>() << url);
    6.  
    7. ...
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Vovasty (30th January 2014)

  4. #3
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: drag and drop: drag item from QListWidget to desktop or any open directory

    Thank you very much for help... it's work!
    Last edited by Vovasty; 30th January 2014 at 06:59.

Similar Threads

  1. to use drag & drop on QListWidget
    By giorgik in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2012, 18:00
  2. Drag and drop between QListWidget's
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2011, 04:29
  3. Replies: 3
    Last Post: 10th June 2010, 15:13
  4. Replies: 1
    Last Post: 22nd October 2009, 00:02
  5. QListWidget Drag and Drop problem
    By winkle99 in forum Qt Programming
    Replies: 0
    Last Post: 20th October 2009, 20:00

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.