Results 1 to 2 of 2

Thread: Dragging text/uri-list: Qt inserts garbage

  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Dragging text/uri-list: Qt inserts garbage

    I need to drag image files (in the form of URLs) to other applications. It should work for
    • Drag to image editor (which will open the image files)
    • Drag to terminal window (which will paste the URLs into the commandline)

    Here is a minimal demo of the problem. It is to be started with the paths to a few local image files as arguments. These will be displayed in a QLabel and can then be dragged.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Foo : public QLabel
    4. {
    5. QPoint dragpos;
    6. QStringList files;
    7. public:
    8. Foo(QStringList data) : QLabel(data.join("\n")), files(data) {setAcceptDrops(true);}
    9. void mousePressEvent(QMouseEvent *ev) {
    10. if (ev->button() == Qt::LeftButton) dragpos = ev->pos();
    11. }
    12. void mouseMoveEvent(QMouseEvent *ev)
    13. {
    14. if ((ev->buttons() & Qt::LeftButton) && (ev->pos() - dragpos).manhattanLength() >= QApplication::startDragDistance()) {
    15. QDrag *drag = new QDrag(this);
    16. QMimeData *mdata = new QMimeData();
    17. QList<QUrl> urlist;
    18. foreach (QString file, files) {
    19. urlist << QUrl::fromLocalFile(file);
    20. }
    21. mdata->setUrls(urlist);
    22. mdata->setText("Plain text version");
    23. qDebug("has text/x-moz-url: %s", mdata->hasFormat("text/x-moz-url") ? "yes":"no"); // says "no"
    24. mdata->removeFormat("text/x-moz-url"); // no effect as isn't there yet
    25. drag->setMimeData(mdata);
    26. drag->start(Qt::CopyAction);
    27. }
    28. }
    29. void dragEnterEvent(QDragEnterEvent *ev) {
    30. ev->acceptProposedAction();
    31. }
    32. void dropEvent(QDropEvent *ev) {
    33. foreach (QString format, ev->mimeData()->formats()) {
    34. qDebug("%s", qPrintable(format));
    35. }
    36. QByteArray ba = ev->mimeData()->data("text/x-moz-url");
    37. qDebug("got x-moz-url = %s", qPrintable(QString::fromRawData((QChar *)(ba.data()), int(ba.size()/sizeof(QChar)))));
    38. }
    39. };
    40.  
    41. int main( int argc, char **argv )
    42. {
    43. QApplication app( argc, argv );
    44. QStringList files;
    45. QDir dir;
    46.  
    47. for (int i=1; i<argc; i++) files.append(dir.absoluteFilePath(argv[i]));
    48.  
    49. Foo foo(files);
    50. foo.show();
    51.  
    52. return app.exec();
    53. }
    To copy to clipboard, switch view to plain text mode 

    Using the program above, I can drag a few image files to the gimp image editor and they will open fine.
    I can also drag the URLs to the konsole terminal program and the filenames will be pasted in, properly quoted.
    But when I drag into gnome-terminal (the default Ubuntu terminal), I only get the first filename, with an extraneous \r\n.
    This is because Qt, in addition to the text/uri-list that I inserted into the QMimeData object, also adds something in text/x-moz-url format (the latter format being preferred by gnome-terminal).
    Start two copies of the program above (both with filename arguments) and drag from one to the other to see what is dragged and what is received.
    So, my questions are:
    • Why does Qt add data in text/x-moz-url format without being asked to?
    • Why does that data only include the first filename? (this link suggests x-moz-url can handle multiple urls)
    • How can I construct a draggable object with text/uri-list but lacking text/x-moz-url?


    (Using Qt-4.8.0-beta)

  2. #2
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Dragging text/uri-list: Qt inserts garbage

    It may not be possible to avoid a text/x-moz-url component in the dragged data, but it is possible to control what it contains, e.g. the following code fragment can be used to define both it and a standard text/uri-list:

    Qt Code:
    1. QList<QUrl> urlist;
    2. QString str;
    3. foreach (QString file, files) {
    4. urlist << QUrl::fromLocalFile(file);
    5. str += QString("'%1' ").arg(file);
    6. }
    7. mdata->setUrls(urlist);
    8. ba.setRawData((char *)str.data(), str.length()*sizeof(QChar));
    9. mdata->setData("text/x-moz-url", ba);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Help QT checkbox and text on list
    By jgaleanog in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2010, 15:40
  2. Replies: 3
    Last Post: 10th June 2010, 16:13
  3. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 13:47
  4. QDataTable Inserts and Updates
    By ederbs in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2006, 00:23
  5. Text box and list box connectivity
    By Rekha in forum Qt Programming
    Replies: 18
    Last Post: 28th July 2006, 13:46

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.