Results 1 to 3 of 3

Thread: How to send filename along with a drag object ?

  1. #1
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Question How to send filename along with a drag object ?

    I have custom model class having a qlist of a custom metatype.
    The custom metatype has a qpixmap & qstring as the member variables. Instances of this metatype are added to model data. The view which uses this model has dragEnabled(true) on its items. Hence when the items from the view are dragged, I want to send the string ( which is stored as part of the metatype ) also. How should I pass this string in the QMimeData that is passed along with the drag object ? Or is there another way to do this ?
    The model's mimeData() method is -

    Qt Code:
    1. QMimeData *pixmapmodel::mimeData(const QModelIndexList &indexes) const
    2. {
    3. QMimeData *mimeData = new QMimeData();
    4. QByteArray encodedData;
    5.  
    6. QDataStream stream(&encodedData, QIODevice::WriteOnly);
    7.  
    8. foreach (QModelIndex index, indexes) {
    9. if (index.isValid()) {
    10. QPixmap pixmap = qVariantValue<QPixmap>(data(index, Qt::UserRole));
    11. stream << pixmap;
    12. }
    13. }
    14.  
    15. mimeData->setData("image/bmp", encodedData);
    16. return mimeData;
    17. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to send filename along with a drag object ?

    Hi,

    You can use a XML document to send data in a structured way for example:

    Qt Code:
    1. QByteArray itemData;
    2.  
    3. QDir directory("/usr/local/home/myhome");
    4.  
    5. QDomDocument doc("myDocument");
    6. QDomElement root = doc.createElement("myDocument");
    7. doc.appendChild(root);
    8.  
    9. QDomElement dirnode = doc.createElement("Directory Name");
    10. root.appendChild(dirnode);
    11. QString value;
    12. value = directory.dirName();
    13. t = doc.createTextNode(value);
    14. dirnode.appendChild(t);
    15.  
    16. itemData.append(doc.toString());
    17.  
    18. QMimeData *mimeData = new QMimeData;
    19. mimeData->setData("object/x-myApplication-object", itemData);
    20.  
    21. QDrag *drag = new QDrag(this);
    22. drag->setMimeData(mimeData);
    To copy to clipboard, switch view to plain text mode 

    Then you can read it by:

    Qt Code:
    1. if (event->mimeData()->hasFormat("object/x-myApplication-object"))
    2. {
    3. QByteArray objectData = event->mimeData()->data("object/x-myApplication-object");
    4. QDomDocument doc("myDocument");
    5. doc.setContent(objectData,true);
    6.  
    7. QDomElement docElem = doc.documentElement();
    8. QDomElement element;
    9. QDomNode node = docElem.firstChild();
    10. element = node.toElement();
    11. QString directory;
    12. directory = element.text();
    13. }
    To copy to clipboard, switch view to plain text mode 

    As you can see the code uses QByteArray. In this array you can put any kind of data (as long as you know how to read it) so you can pass text, images, etc. So for example you can pass an structure like [lengthOfXml][xmlData][lengthOfImage][imageData]
    Last edited by qlands; 4th October 2010 at 12:38.

  3. #3
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Question Re: How to send filename along with a drag object ?

    Oh , ok will need to try this out. Thanks

Similar Threads

  1. Replies: 4
    Last Post: 20th August 2010, 13:07
  2. Replies: 3
    Last Post: 10th June 2010, 15:13
  3. how to get filename
    By jayreddy in forum Qt Programming
    Replies: 1
    Last Post: 24th November 2009, 08:59
  4. Replies: 3
    Last Post: 14th October 2008, 21:04
  5. How can I get a filename from a different encoding?
    By Pepe in forum Qt Programming
    Replies: 4
    Last Post: 28th March 2007, 00:18

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.