python - how to sort the items in QListWidget by drop and drag -
i newer pyqt, using write gui, please tell me how sort items in qlistwidget drop , drag?
thanks in advance
qlistwidget
inherits qabstractitemview
. can use qabstractitemview.setdragdropmode()
, set qabstractitemview.internalmove
if you'd able change order of items drag & drop.
here's relevent section of documentation.
here's quick example showing in action:
import sys pyqt4.qtgui import qapplication, qwidget, \ qvboxlayout, qlistwidget, qabstractitemview class widget(qwidget): def __init__(self, parent=none): qwidget.__init__(self, parent) self.widget_layout = qvboxlayout() # create listwidget , add 10 items move around. self.list_widget = qlistwidget() x in range(1, 11): self.list_widget.additem('item {:02d}'.format(x)) # enable drag & drop ordering of items. self.list_widget.setdragdropmode(qabstractitemview.internalmove) self.widget_layout.addwidget(self.list_widget) self.setlayout(self.widget_layout) if __name__ == '__main__': app = qapplication(sys.argv) widget = widget() widget.show() sys.exit(app.exec_())