Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-49981

Improve drag and drop documentation

    XMLWordPrintable

Details

    Description

      The drag and drop API is... something else. I personally find it very unintuitive and difficult to use, so I often go to the documentation. For whatever reason, the person who added this API did not add sufficient documentation.

      I can give one concrete example. http://doc.qt.io/qt-5/qml-qtquick-drag.html#keys-attached-prop says:

      This property holds a list of keys that can be used by a DropArea to filter drag events.

      Changing the keys while a drag is active will reset the sequence of drag events by sending a drag leave event followed by a drag enter event with the new source.

      Ok, but what is a key? Maybe DropArea's documentation can shed some light:

      This property holds a list of drag keys a DropArea will accept.

      If no keys are listed the DropArea will accept events from any drag source, otherwise the drag source must have at least one compatible key.

      See also QtQuick::Drag::keys.

      Nope. This doesn't tell me anything.

      For example, if I wanted to test if external drag and drop actually works in Qt by printing something when a certain file type was dropped over a DropArea, how would I do it?

      If we take the example at http://doc.qt.io/qt-5/qtquick-externaldraganddrop-example.html, simplify it, and add a print statement to the onDropped handler and change the keys property to accept the mime type we're interested in:

      import QtQuick 2.3
      import QtQuick.Window 2.2
      import QtQuick.Layouts 1.0
      
      Window {
          visible: true
          x: 0
          y: 0
          width: 400
          height: 200
          color: "black"
      
          Item {
              id: item
              anchors.fill: parent
      
              Text {
                  anchors.centerIn: parent
                  color: "white"
                  text: "Drop something on me!"
              }
              DropArea {
                  anchors.fill: parent
                  keys: ["image/jpeg"]
                  onDropped: {
                      print(drop.formats)
                  }
              }
          }
      }
      

      It doesn't print anything out. If you remove the keys assignment altogether, then you will get output:

      application/x-qt-windows-mime;value="Shell IDList Array",application/x-qt-windows-mime;value="FileOpFlags",application/x-qt-windows-mime;value="UsingDefaultDragImage",application/x-qt-windows-mime;value="DragImageBits",application/x-qt-windows-mime;value="DragContext",application/x-qt-windows-mime;value="DragSourceHelperFlags",application/x-qt-windows-mime;value="InShellDragLoop",text/uri-list,application/x-qt-windows-mime;value="FileName",application/x-qt-windows-mime;value="FileContents",application/x-qt-windows-mime;value="FileNameW",application/x-qt-windows-mime;value="FileGroupDescriptorW",application/x-qt-windows-mime;value="IsShowingLayered",application/x-qt-windows-mime;value="DragWindow",application/x-qt-windows-mime;value="IsComputingImage",application/x-qt-windows-mime;value="DisableDragText",application/x-qt-windows-mime;value="IsShowingText",application/x-qt-windows-mime;value="DropDescription",application/x-qt-windows-mime;value="ComputedDragImage",application/x-qt-windows-mime;value="Preferred DropEffect"
      

      So, which one would I use? I just want to allow image drops.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              docteam Qt Documentation Team
              mitch_curtis Mitch Curtis
              Votes:
              7 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes