chacham15
chacham15

Reputation: 14251

How can I drag to the desktop with Qt?

Similar to Qt 4.x: how to implement drag-and-drop onto the desktop or into a folder? but without the delayed data bit. I have all of the data in memory and have the filename that I want to save the file as. The following code does not work, however:

QDrag *drag = new QDrag(this);

QMimeData *mimeData = new QMimeData;
mimeData->setData(QString::fromUtf8("text/uri-list"), QByteArray(filename, utf8_datalen(filename)));
QByteArray data(buffer->data, buffer->current_location);
mimeData->setData("application/octet-stream", data);

drag->setMimeData(mimeData);

Qt::DropAction dropAction = drag->exec(Qt::CopyAction);

The cursor changes properly when I drag outside of the application. However, after dropping, nothing happens; no file is copied. What am I doing wrong?

Side note: I want to build a cross-platform solution, but I am currently testing with windows.

Upvotes: 1

Views: 1173

Answers (1)

OnWhenReady
OnWhenReady

Reputation: 949

As far as i know there is no cross-platform solution without using temporary files. For a Windows-only solution you must set the appropriate MIME data:

if(mimeType == "FileName")
{
    QString filename;
    // YOUR CODE HERE
    mimeData->setData("FileName", filename.toLatin1());
}

if(mimeType == "FileNameW")
{
    QString filename;
    // YOUR CODE HERE
    mimeData->setData("FileNameW", QByteArray((const char*) (filename.utf16()), DDFileName.size() * 2));
}
if(mimeType == "FileContents")
{
    QByteArray data;
    // YOUR CORE HERE
    mimeData->setData("FileContents", data);
}
if(mimeType == "FileGroupDescriptorW")
{
    QString filename;
    // YOUR CODE HERE
    FILEGROUPDESCRIPTOR desc;
    desc.cItems = 1;
    desc.fgd[0].dwFlags = FD_PROGRESSUI;
    wcscpy_s(desc.fgd[0].cFileName, filename.toStdWString().c_str());
    // make a deep copy here
    mimeData->setData("FileGroupDescriptorW", QByteArray((const char*)&desc,
               sizeof(FILEGROUPDESCRIPTOR)));
}

Upvotes: 1

Related Questions