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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| #!/usr/bin/env python
# vim: set expandtab tabstop=4 shiftwidth=4:
# Qt5 - Drag-and-Drop Reordering of Rows in a QTableView
# http://apocalyptech.com/linux/qt/qtableview/
# =======================================
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class MyModel(QtGui.QStandardItemModel):
def dropMimeData(self, data, action, row, col, parent):
"""
Always move the entire row, and don't allow column "shifting"
"""
return super().dropMimeData(data, action, row, 0, parent)
class MyStyle(QtWidgets.QProxyStyle):
def drawPrimitive(self, element, option, painter, widget=None):
"""
Draw a line across the entire row rather than just the column
we're hovering over. This may not always work depending on global
style - for instance I think it won't work on OSX.
"""
if element == self.PE_IndicatorItemViewItemDrop and not option.rect.isNull():
option_new = QtWidgets.QStyleOption(option)
option_new.rect.setLeft(0)
if widget:
option_new.rect.setRight(widget.width())
option = option_new
super().drawPrimitive(element, option, painter, widget)
class MyTableView(QtWidgets.QTableView):
def __init__(self, parent):
super().__init__(parent)
self.verticalHeader().hide()
self.horizontalHeader().hide()
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
self.setSelectionBehavior(self.SelectRows)
self.setSelectionMode(self.SingleSelection)
self.setShowGrid(False)
self.setDragDropMode(self.InternalMove)
self.setDragDropOverwriteMode(False)
# Set our custom style - this draws the drop indicator across the whole row
self.setStyle(MyStyle())
# Set our custom model - this prevents row "shifting"
self.model = MyModel()
self.setModel(self.model)
catalogue=QtWidgets.QFileDialog.getOpenFileNames(self,"Sélection de fichier(s)","/home/","*")
# for (idx, data) in enumerate(['foo', 'bar', 'baz']):
# item_1 = QtGui.QStandardItem('Item {}'.format(idx))
# item_1.setEditable(False)
# item_1.setDropEnabled(False)
# item_2 = QtGui.QStandardItem(data)
# item_2.setEditable(False)
# item_2.setDropEnabled(False)
for F in catalogue[0]:
i=catalogue[0].index(F)
title=F.split("/")[-1][0:-4]
# item_1 = QtGui.QStandardItem('{}'.format(i))
# item_1.setEditable(False)
# item_1.setDropEnabled(False)
item_2 = QtGui.QStandardItem(title)
item_2.setEditable(False)
item_2.setDropEnabled(False)
self.model.appendRow([item_2])
for i in range(0, self.model.rowCount()):
print(self.model.item(i).data(QtCore.Qt.DisplayRole))
# data2 = [str(ch) for ch in list(self.model().stringList())]
# print(data2)
class Testing(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# Main widget
w = QtWidgets.QWidget()
l = QtWidgets.QVBoxLayout()
w.setLayout(l)
self.setCentralWidget(w)
# ajout d'un bouton pour valider la reorganisation
saveCatalogButton=QtWidgets.QPushButton("Enregistre le catalogue")
saveCatalogButton.clicked.connect(self.saveCatalog)
# spacer
# l.addWidget(QtWidgets.QLabel('top'), 1)
# Combo Box
l.addWidget(MyTableView(self))
# spacer
l.addWidget(saveCatalogButton)
# A bit of window housekeeping
self.resize(600, 400)
self.setWindowTitle('Testing')
# Sauvegarde du catalogue vers .... une sortie fichier ou autre
def saveCatalog(self):
print("bonjour - le bouton est clqué")
print( "le nouveau catalogue")
if __name__ == '__main__':
app = QtWidgets.QApplication([])
test = Testing()
test.show()
sys.exit(app.exec_()) |
Partager