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.
Code:
#include <QtGui>
{
public:
if (ev->button() == Qt::LeftButton) dragpos = ev->pos();
}
{
if ((ev
->buttons
() & Qt
::LeftButton) && (ev
->pos
() - dragpos
).
manhattanLength() >
= QApplication::startDragDistance()) { QList<QUrl> urlist;
urlist <<
QUrl::fromLocalFile(file);
}
mdata->setUrls(urlist);
mdata->setText("Plain text version");
qDebug("has text/x-moz-url: %s", mdata->hasFormat("text/x-moz-url") ? "yes":"no"); // says "no"
mdata->removeFormat("text/x-moz-url"); // no effect as isn't there yet
drag->setMimeData(mdata);
drag->start(Qt::CopyAction);
}
}
ev->acceptProposedAction();
}
foreach
(QString format, ev
->mimeData
()->formats
()) { qDebug("%s", qPrintable(format));
}
QByteArray ba
= ev
->mimeData
()->data
("text/x-moz-url");
qDebug
("got x-moz-url = %s", qPrintable
(QString::fromRawData((QChar *)(ba.
data()),
int(ba.
size()/sizeof(QChar)))));
}
};
int main( int argc, char **argv )
{
for (int i=1; i<argc; i++) files.append(dir.absoluteFilePath(argv[i]));
Foo foo(files);
foo.show();
return app.exec();
}
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)
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:
Code:
QList<QUrl> urlist;
urlist <<
QUrl::fromLocalFile(file);
}
mdata->setUrls(urlist);
ba.
setRawData((char *)str.
data(), str.
length()*sizeof(QChar));
mdata->setData("text/x-moz-url", ba);