Ask Your Question
1

Can you assist with managing the rearrangement of items through drag-and-drop on multiple pages in QML?

asked 2022-10-16 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-12-17 14:00:00 +0000

djk gravatar image

Yes, I can give you some guidance on how to manage drag-and-drop for multiple pages in QML:

  1. Define the drag and drop areas on each page by creating a Rectangle element with the properties drag.target: true and drop.target: true. Also, assign an id to each Rectangle to be able to reference it later.
Rectangle {
    id: page1DragDropArea
    width: 200
    height: 200
    drag.target: true
    drop.target: true
    // other properties
}

Rectangle {
    id: page2DragDropArea
    width: 200
    height: 200
    drag.target: true
    drop.target: true
    // other properties
}
  1. Define the items to be rearranged by creating the necessary elements inside each drag-and-drop area. Assign an id to each item to be able to reference it later.
Rectangle {
    id: item1
    width: 50
    height: 50
    // other properties
}

Rectangle {
    id: item2
    width: 50
    height: 50
    // other properties
}

// other items
  1. Define the behavior of the drag-and-drop event by implementing the necessary functions in the onDrag and onDrop properties of each rectangle. In the onDrag function, set the drag properties of the dragged item. In the onDrop function, change the position of the dropped item and any other necessary changes.
page1DragDropArea.onDrag: {
    draggedItem.source = item1;
    draggedItem.position.x = item1.x;
    draggedItem.position.y = item1.y;
}

page2DragDropArea.onDrop: {
    if (draggedItem.source.id === "item1") {
        item1.position.x = draggedItem.position.x;
        item1.position.y = draggedItem.position.y;
    } else if (draggedItem.source.id === "item2") {
        item2.position.x = draggedItem.position.x;
        item2.position.y = draggedItem.position.y;
    }
}

// other onDrag and onDrop functions
  1. Add the DragHandler component to your QML file to enable drag-and-drop functionality. Configure it by setting the target property to the id of the drag-and-drop area and the delegate property to a delegate element that defines the drag appearance of the dragged item.
DragHandler {
    target: page1DragDropArea
    delegate: Rectangle {
        width: 50
        height: 50
        color: "red"
    }
}
  1. Repeat steps 3 and 4 for each drag-and-drop area and item, adjusting them as necessary.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-10-16 11:00:00 +0000

Seen: 11 times

Last updated: Dec 17 '21