qt - How to detect that an item in a QStandardItemModel is dropped onto another? -
how receive notification qstandarditem dragged , dropped onto qstandarditem, becoming child of latter?
i thought re-implementing qstandarditemmodel.moverows, not getting called after drop :( below example of i'm trying do. test, run program , drop 1 item in tree view onto another. if worked, should see confirmation in console moverows has been called.
from pyqt5.qtgui import * pyqt5.qtwidgets import * pyqt5.qtcore import * class model(qstandarditemmodel): def moverows( self, source_parent, source_row, count, destination_parent, destination_child): print( 'moving {} row(s) row {} of parent {} row {} of parent {}' .format( count, source_row, source_parent, destination_child, destination_parent) ) super().moverows( source_parent, source_row, count, destination_parent, destination_child) return true def _create_item(text): item = qstandarditem(text) flags = qt.itemisdragenabled | qt.itemisselectable | qt.itemisenabled | qt.itemisdropenabled item.setflags(flags) return item model = model() model.appendrow([_create_item('item 1')]) model.appendrow([_create_item('item 2')]) app = qapplication([]) view = qtreeview() view.setdragdropmode(view.internalmove) view.setmodel(model) view.show() app.exec_()
from studying qstandarditemmodel source code, realized 1 has override qabstractitemmodel.dropmimedata, in order react item drops. qstandarditemmodel moves items around in implementation of dropmimedata, in way subclass can't hook into, have handle dropmimedata yourself.
this dropmimedata based solution, item representations included mime data python pickles:
import pickle pyqt5.qtgui import * pyqt5.qtwidgets import * pyqt5.qtcore import * _mimetype = 'application/x-standarditemmodeldatalist' class model(qstandarditemmodel): def dropmimedata( self, data, action, row, column, parent): # access parent before calling super implementation, because may mutate parent dest_str = 'parent {}'.format(parent.data(qt.displayrole)) if \ parent.isvalid() else 'root' ret_val = super().dropmimedata(data, action, row, column, parent) if action != qt.moveaction or not data.hasformat(_mimetype): return ret_val item_data = pickle.loads(data.data(_mimetype))[0] print('moving {} {}'.format(item_data, dest_str)) return true # override in order add custom mime data def mimedata(self, indexes): mimedata = super().mimedata(indexes) data = [index.data(qt.displayrole) index in indexes] mimedata.setdata(_mimetype, pickle.dumps(data)) return mimedata # override in order announce our custom mime type def mimetypes(self): return super().mimetypes() + [_mimetype] def _create_item(text): item = qstandarditem(text) flags = qt.itemisdragenabled | qt.itemisselectable | qt.itemisenabled | qt.itemisdropenabled item.setflags(flags) return item model = model() model.appendrow([_create_item('item 1')]) model.appendrow([_create_item('item 2')]) app = qapplication([]) view = qtreeview() view.setdragdropmode(view.internalmove) view.setmodel(model) view.show() app.exec_()
Comments
Post a Comment