Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag web link to desktop
Forum Updated to NodeBB v4.3 + New Features

Drag web link to desktop

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SorenHansen
    wrote on last edited by SorenHansen
    #1

    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

    raven-worxR 1 Reply Last reply
    0
    • S SorenHansen

      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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      S 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @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.

        S Offline
        S Offline
        SorenHansen
        wrote on last edited by
        #3

        @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).

        raven-worxR 1 Reply Last reply
        0
        • S SorenHansen

          @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).

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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 once retrieveData() 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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          S 1 Reply Last reply
          3
          • raven-worxR raven-worx

            @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 once retrieveData() 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.

            S Offline
            S Offline
            SorenHansen
            wrote on last edited by
            #5

            @raven-worx Yeah, that may be even easier. Thanks.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved