1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
# Python 2.7
import sys
from PyQt4 import QtCore, QtGui
#############################################################################
class Fenetre(QtGui.QWidget):
def __init__(self, parent=None):
super(Fenetre, self).__init__(parent)
self.setWindowTitle('Drag & Drop sur une QListWidget')
self.lw = QtGui.QListWidget(self)
self.datas = [u"toto1", u"toto2", u"toto3", u"toto4", u"toto5", u"toto6", u"toto7", u"toto8", u"toto9"]
self.lw.addItems(self.datas)
# détournement de la méthode dropEvent du QListWidget
self.lw.dropEvent = self.dropEvent_lw
# mode Drag&Drop sur le changement d'ordre (sans ajout ni effacement)
self.lw.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
posit = QtGui.QGridLayout()
posit.addWidget(self.lw, 0, 0)
self.setLayout(posit)
def dropEvent_lw(self, event):
"""appelé à chaque modif d'ordre"""
lw_item = self.lw.currentItem()
lw_row = self.lw.row(lw_item)
lw_data = lw_item.text()
QtGui.QListWidget.dropEvent(self.lw, event)
print u"dropEvent: l'item", lw_data, u"est passé de l'index", lw_row, u"à l'index", self.lw.row(self.lw.currentItem())
#############################################################################
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
fen = Fenetre()
fen.show()
app.exec_() |
Partager