Drag and Drop between two QListWidgets PyQt5 -
have 2 qlistwidgets. want able move items widget 1 onto widget 2 , vice versa dragging , dropping. must work multiselection mode. must moveaction not copy action. simple way achieve use:
self.listwidget_2.setdragdropmode(qtwidgets.qabstractitemview.dragdrop) self.listwidget_2.setdefaultdropaction(qtcore.qt.moveaction) self.listwidget_2.setselectionmode(qtwidgets.qabstractitemview.multiselection) self.listwidget_2.setobjectname("listwidget_2") self.listwidget_2.acceptdrops()
this achieves desired requirements 1 caveat. dragging , dropping item onto widget resides removes widget. definite no go. tried write own qlistwidget class achieve desired results no avail, class:
class dragleavelist(qtwidgets.qlistwidget): def __init__(self, type, parent=none): super(dragleavelist, self).__init__(parent) self.setdragdropmode(qtwidgets.qabstractitemview.dragdrop) self.setselectionmode(qtwidgets.qabstractitemview.multiselection) self.setalternatingrowcolors(true) self.setacceptdrops(true) def dragenterevent(self, event): if event.mimedata().hastext(): event.accept() print("drag enter event: if clause") else: super(dragleavelist, self).dragenterevent(event) print("drag enter event: else clause") def dragmoveevent(self, event): if event.mimedata().hastext(): event.setdropaction(qtcore.qt.moveaction) event.accept() else: event.ignore() super(dragleavelist, self).dragmoveevent(event) def dropevent(self, event): print("drop event ", event) if event.mimedata().hastext(): event.setdropaction(qtcore.qt.moveaction) event.accept() print("drop event - 1 ", event) def startdrag(self, event): print("in start drag") item = self.currentitem() itemtext = self.currentitem().text() itemdata = qtcore.qbytearray() datastream = qtcore.qdatastream(itemdata, qtcore.qiodevice.writeonly) print(item, itemtext) mimedata = qtcore.qmimedata() mimedata.setdata(itemtext, itemdata) drag = qtgui.qdrag(self) drag.setmimedata(mimedata) if drag.exec_(qtcore.qt.moveaction) == qtcore.qt.moveaction: if self.currentitem() not none: self.takeitem(self.row(item))
i've read every tutorial online , related documentation on sourceforge , can't seem make work. end not being able figure out documentation seems pretty darn bad. i'm looking explanations along code. however, said i've read related documentation multiple times i'm not ignorant. seems me in above class have lot more need , may have right components not right implementation. please give me clarity on subject, bunch.
main things clarify in answer code
- the bare minimum/most effective dragdrop(the built in methods i.e. dragenterevent, dragmoveevent et cetera) methods.
- is mimedata() absolutely required? assume yes docs.
- formatting mimedata() qlistwidgetitem.
- does item have manually removed widget or built in qtcore.qt.moveaction?
- actually making qtcore.qt.moveaction move list item.
so have figured out 1 solution in working order. keep in mind answer not teach me wanted know. however, may helpful others wishing same or similar. i'm still looking method touches on topics listed in " main things clarify code" list.
class qlistdraganddrop(qtwidgets.qlistwidget): def __init__(self): super(qlistdraganddrop, self).__init__() self.setframeshape(qtwidgets.qframe.winpanel) self.setframeshadow(qtwidgets.qframe.raised) self.setdragenabled(true) self.setdragdropmode(qtwidgets.qabstractitemview.dragdrop) self.setdefaultdropaction(qtcore.qt.moveaction) self.setselectionmode(qtwidgets.qabstractitemview.multiselection) self.setmovement(qtwidgets.qlistview.snap) self.setproperty("iswrapping", true) self.setwordwrap(true) self.setsortingenabled(true) self.setacceptdrops(true)
just make qlistwidget class these parameters set , import wherever need works charm not teach much. hopefully, helps people until answers question properly.
Comments
Post a Comment