qt - When dragging multiple items from QListWidget, non-draggable items get removed -
i have 2 qlistwidgets. user can select multiple items 1 list , drag them other list. within each list, items draggable , not. if selection contains both draggable , non-draggable items, problem happens. draggable items appear in second list, correct. all items disappear first list. 
in animated image above, items 00, 01, , 02 selected. items 00 , 02 drag enabled. after drag-and-drop, 3 items gone first list. how can fix this?
here code reproduce problem:
import random import sys pyside import qtcore, qtgui class testmultidragdrop(qtgui.qmainwindow): def __init__(self, parent=none): super(testmultidragdrop, self).__init__(parent) centralwidget = qtgui.qwidget() self.setcentralwidget(centralwidget) layout = qtgui.qhboxlayout(centralwidget) self.list1 = qtgui.qlistwidget() self.list1.setdragdropmode(qtgui.qabstractitemview.dragdrop) self.list1.setdefaultdropaction(qtcore.qt.moveaction) self.list1.setselectionmode(qtgui.qlistwidget.extendedselection) self.list2 = qtgui.qlistwidget() self.list2.setdragdropmode(qtgui.qabstractitemview.dragdrop) self.list2.setdefaultdropaction(qtcore.qt.moveaction) self.list2.setselectionmode(qtgui.qlistwidget.extendedselection) layout.addwidget(self.list1) layout.addwidget(self.list2) self.filllistwidget(self.list1, 8, 'someitem') self.filllistwidget(self.list2, 4, 'anotheritem') def filllistwidget(self, listwidget, numitems, txt): in range(numitems): item = qtgui.qlistwidgetitem() newtxt = '{0}{1:02d}'.format(txt, i) if random.randint(0, 1): item.setflags(qtcore.qt.itemisselectable | qtcore.qt.itemisenabled) else: # if item draggable, indicate * newtxt += ' *' item.setflags(qtcore.qt.itemisselectable | qtcore.qt.itemisenabled | qtcore.qt.itemisdragenabled) item.settext(newtxt) listwidget.additem(item) def openmultidragdrop(): global multidragdropui try: multidragdropui.close() except: pass multidragdropui = testmultidragdrop() multidragdropui.setattribute(qtcore.qt.wa_deleteonclose) multidragdropui.show() return multidragdropui if __name__ == '__main__': app = qtgui.qapplication([]) openmultidragdrop() sys.exit(app.exec_())
here have suspicion on setdefaultdropaction(qtcore.qt.moveaction)
read below para documentation: specially bold line
in simplest case, target of drag , drop action receives copy of data being dragged, , source decides whether delete original. described copyaction action. target may choose handle other actions, moveaction , linkaction actions. if source calls qdrag::exec(), , returns moveaction, source responsible deleting original data if chooses so. qmimedata , qdrag objects created source widget should not deleted - destroyed qt.
(http://doc.qt.io/qt-4.8/dnd.html#overriding-proposed-actions)
first give try qtcore.qt.copyaction
second, if moveaction mandatory, try creating qmimedata , qdrag objects in source list widget's mousemoveevent.
here in below link, can find creating qmimedata , qdrag objects in source list widget's mousemoveevent. (code in c++, intention conceptual idea).
http://doc.qt.io/qt-4.8/dnd.html#overriding-proposed-actions
Comments
Post a Comment