Solved Drag web link to desktop
-
Hi folks,
I'm trying to figure out how to drag a web link on to the desktop (on various platforms, Linux, OS/X, Win) using QDrag/QMimeData. I've tried using the MIME type text/uri-list and setting the URLs through QMimeData::setUrls. However this causes the desktop to try do download the given URL to a local file instead of creating a shortcut. I need the same result as when you drag a link from a web browser onto the dashboard.
Is there a platform-independent way to do this in QT, or do I need to set up the contents of the .desktop / .url / ... file myself platform by platform?
Thanks for any input on this
-- Soren -
@SorenHansen
By inspecting the drop data (on Windows) you need to to the following to create a shortcut link via Drop into the filesystem:#ifdef Q_OS_WIN #include <Shlobj.h> QString fileName = "shortcut.url"; FILEGROUPDESCRIPTOR desc; desc.cItems = 1; desc.fgd[0].dwFlags = FD_PROGRESSUI; wcscpy_s(desc.fgd[0].cFileName, fileName.toStdWString().c_str()); data->setData("FileGroupDescriptorW", QByteArray((const char*)&desc, sizeof(FILEGROUPDESCRIPTOR))); data->setData("FileGroupDescriptor", data->data("FileGroupDescriptorW") ); const char* d = "[InternetShortcut]\r\n" "URL=https://link.tld\r\n" "IDList=\r\n" "HotKey=0\r\n" "IconFile=C:\\path\\to\\icon.ico\r\n" "IconIndex=0\r\n"; data->setData("FileContents", d); #endif
Basically this just copies a .url file to the filesystem.
For the FileContents type it's also enough just to provide the URL and IDList fields, or maybe just even the URL field alone. Give it a try.
I think this is very OS and filemanager specific, so you need to to check what the drop data on the other platforms looks like.
E.g. the Qt examples come with "DropSite" example which you can use for that. -
@raven-worx Thanks, I will look into that, and probably make the relevant platform specific versions. Must see if I can find a way to hint a file name on Linux when using setData.
I had hoped that a text/uri-list + QT::LinkAction would do the trick. I haven't tried it on Windows or MacOS, but on Linux/xfce it does not seem to work (nothing happens (no shortcut is created), while CopyAction downloads the actual file as expected).
-
@SorenHansen
another idea what just came into my mind:Following the Delayed encoding example you can specify a path to a temporary shortcut file which is then simply copied.
This temporary shortcut file is created onceretrieveData()
is called. The OS then simply copies this temp file to the drop position.In case of windows this would be the data from FileContents above.
-
@raven-worx Yeah, that may be even easier. Thanks.