Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Drag and Drop + MIME Data
Forum Updated to NodeBB v4.3 + New Features

Drag and Drop + MIME Data

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 3.8k Views 1 Watching
  • 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.
  • MrBoltonM Offline
    MrBoltonM Offline
    MrBolton
    wrote on last edited by
    #1

    Hello there,

    my goal is to implement a Drag'n'Drop mechanism as part of a bigger application. Basically I use a QQuickWidget to display a Flickable which is filled by a Repeater with multiple items. Some of these items need to interact with each other by dragging one of them onto another. This should trigger a C++ method which needs data about the source of the drag.

    The best approach would be to put the relevant data into the DragEvent as MIME Data right?
    Now the problem is that my DropAreas accept the drops but there is no data in the DragEvent that is available in the onDropped() slot.

    This is the code for the items that need to be dragged:

    Movable.qml
    @import QtQuick 2.0

    Location {
    x: StationPosXCoord
    y: StationPosYCoord
    z: 10

    locationColor: "#fed505"
    Behavior on y {
        NumberAnimation { }
    }
    
    Behavior on x {
        NumberAnimation { }
    }
    
    Drag.active: dragArea.drag.active
    Drag.proposedAction: Qt.CopyAction
    Drag.hotSpot: Qt.point(dragArea.mouseX, dragArea.mouseY)
    Drag.mimeData: { "text/plain": "asdf" }
    
    MouseArea {
        id: dragArea
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton
        drag.target: parent
        drag.filterChildren: true
        onReleased: drag.target.Drag.drop()
    }
    

    }@

    And this is some of the code that uses the DropArea:

    Location.qml
    @import QtQuick 2.0

    Location {

    Rectangle {
        anchors.fill: parent
        color: locationColor
        border.color: "black"
        border.width: borderWidth
    
        opacity: dropArea.containsDrag ? 0.5 : 1
    
        DropArea {
            id: dropArea
            anchors.fill: parent
    
            onEntered: print("entered");
            onExited: print("exited");
            onDropped: {
                drop.acceptProposedAction()
                print("dropped")
                print(drop.text)
            }
        }@
    

    The opacity of the Rectangle that contains the DropArea is reacting properly and also the onDropped() slot is called. The weird thing is that the attached properties like "proposedAction" and "hotSpot" behave as expected, but the "mimeData" one does not since the output of drop.text is empty.

    Am I missing something or doing something wrong? Any help is much appreciated!

    Cheers, Tobi

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Tân Ngọc Đỗ
      wrote on last edited by
      #2

      Well, I think you can get the mouse press and positionChanged signal on QML and process the QDrag on C++ . It's the acceptable solution now

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dynamo72
        wrote on last edited by
        #3

        FYI: accessing the Drag.mimeData by drop.text works for me with Qt 5.5 now. This way I can also drag and drop to external text editors, for example.

        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