PDA

View Full Version : Drag and Drop different item text in two QListWidgets



Mr_workalota787
10th June 2020, 01:28
I have two QListWidgets , one named “Commands” on the left side and other named “XML Script” on the right side , I am able to drag and drop items from one List to another, How ever, when I drag one item, I don’t want drop that same item text, I want some changed text to be dropped.

Example: In this example when I dragged “SetScroll Command” from “commands” QListWidget and I am trying to drop this in “XML Script” QListWidget , I need some different text to be dropped like If I drag ‘Pause Command’ from “Commands”, the dropped item in “XML Script ”should have item with text like “This is a Pause Command”

?? How can I achieve this, I am intending to keep a Map data structure which will have key as “Pause Command” and Value as “This is a Pause Command”. Whenever key text is dragged , value text shall be dropped,

Any help appreciated.

13469

d_stranz
10th June 2020, 17:16
I think from your previous post, you are already overriding mousePressEvent in your list widget to create a custom QDrag instance with the proper text mime type. Do the same thing here, except replace the list widget text with your XML string. If you will be dragging from the list widget into multiple other widgets, then you can add both "text/plain" and "text/xml" mime types to your QMimeData and select the one you want when you handle the drop event.

In the XML list widget, you will need to write code to handle the dropEvent() and / or dragEnterEvent() to look for the "text/xml" type. If the mime data doesn't have it, you don't accept the drop. This will prevent drops of unrelated text dragged from somewhere else. You can handle these events by installing an event filter on the XML list widget.