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
|
import sys, os
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QTreeView, QMainWindow, qApp, QAction, QPushButton
from PyQt5.QtGui import QStandardItemModel, QStandardItem, QIcon
REMOVEFN = os.path.join(os.path.dirname(__file__), "delete.jpg")
class FenPrincipale(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.model = QStandardItemModel()
item1=QStandardItem("John")
item2=QStandardItem("Paul")
item3=QStandardItem("Vincent")
self.model.appendRow(item1)
self.model.appendRow(item2)
self.model.appendRow(item3)
item1.appendRow(QStandardItem("17 ans"))
self.vue = QTreeView(self)
self.vue.setModel(self.model)
layout = QVBoxLayout()
layout.addWidget(self.vue)
self.setLayout(layout)
# création d'une barre de menu
actExit = QAction('Exit', self)
actExit.triggered.connect(qApp.quit)
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
fileMenu.addAction(actExit)
# créer un icône dans la barre d'outils
exitAct = QAction(QIcon(REMOVEFN), 'remove', self)
exitAct.triggered.connect(self.deletetreerow)
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAct)
# ajouter une fenêtre
w=QWidget()
w.setLayout(layout)
self.setCentralWidget(w)
# fonction pour supprimer un élément dans l'arbre
def deletetreerow(self):
items = self.vue.selectedIndexes()
if len(items) == 0:
print('auncun item sélèctionné')
return
# item = self.model.itemFromIndex(items[0])
self.model.removeRow(items[0].row(), items[0].parent())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = FenPrincipale()
window.show()
sys.exit(app.exec_()) |
Partager