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

Qml drag and drop shift items positions on drop cancel

    XMLWordPrintable

    Details

    • Platform/s:
      Linux/X11

      Description

      Tried to deal with drag and drop in Qml, I discovered cancelled drag / drop operations would shift items on their x/y axis while cancelling the operation.

      Just drag a button from the testcase and let drop it anywhere, the original x,y position of the item would change for no reason.

      import QtQuick 2.12
      import QtQuick.Window 2.12
      import QtQuick.Controls 2.12
      
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          Row {
              height: 50
      
              anchors {
                  left: parent.left
                  top: parent.top
                  right: parent.right
              }
      
              Repeater {
                  id: repeater
      
                  model: 6
      
                  anchors {
                      fill: parent
                  }
      
                  delegate: Control {
                      id: control
      
                      readonly property int index: model.index
      
                      implicitWidth: parent.width / repeater.count
                      implicitHeight: parent.height
      
                      background: Rectangle {
                          radius: 15
                          color: 'red'
                      }
      
                      contentItem: Label {
                          text: qsTr('Button %1').arg(model.index)
                          horizontalAlignment: Label.AlignHCenter
                      }
      
                      MouseArea {
                          id: touchArea
      
                          clip: true
                          hoverEnabled: true
      
                          drag {
                              target: control
                              axis: Drag.XAxis
                          }
      
                          anchors {
                              fill: parent
                          }
      
                          onWheel: {
                              // So event is forwarded
                              wheel.accepted = false;
                          }
      
                          onPressedChanged: {
                              if (pressed) {
                                  control.grabToImage(function(result) {
                                      control.Drag.imageSource = result.url;
                                  });
                              }
                          }
                      }
      
                      Drag.active: touchArea.drag.active
                      Drag.dragType: Drag.Automatic
                      Drag.keys: [ 'TableViewHeader-dnd' ]
                      Drag.mimeData: {
                          'TableViewHeader-dnd': control.index
                      }
                      Drag.supportedActions: Qt.CopyAction
                      Drag.proposedAction: Qt.CopyAction
                      Drag.onDragFinished: {
                          // An annoying bug is shifting items 1px right when cancelling a drop operation...
                          // Let unshift...
      //                    Qt.callLater(function() {
      //                        control.x = control.x - 1;
      //                        console.log('okok');
      //                    });
                      }
      
                      DropArea {
                          id: drop
      
                          keys: control.Drag.keys
      
                          onEntered: {
                              drag.accepted = drag.source !== control;
                          }
      
                          onDropped: {
                              drag.accepted = false;
                          }
      
                          anchors {
                              fill: parent
                          }
                      }
      
                      opacity: drop.containsDrag ? 0.5 : 1.0
      
                      Keys.onEscapePressed: {
                          control.Drag.cancel();
                      }
                  }
              }
          }
      }
      
      

        Attachments

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

          Activity

            People

            Assignee:
            qt.team.quick.subscriptions Qt Quick and Widgets Team
            Reporter:
            filipe.azevedo Filipe Azevedo
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes