Le composant QTableView
implémente une architecture de type MVC. Il est nécessaire d'associer à votre vue (le widget de type QTableView
) un modèle de données
(une instance d'une classe dérivant de QAbstractTableModel
). Voici un exemple simple d'utilisation.
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 |
#!/usr/bin/python3 # -*- coding : utf-8 -*- import sys from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import * # A window class class MyWindow(QWidget): def __init__(self, *args): QWidget.__init__(self, *args) # The raw data my_array = [['00','01','02'], ['10','11','12'], ['20','21','22']] # Create the QTableView widget and associate to its model tablemodel = MyTableModel(my_array, self) tableview = QTableView() tableview.setModel(tablemodel) # Place the table widget into a layout layout = QVBoxLayout(self) layout.addWidget(tableview) self.setLayout(layout) # The table model class class MyTableModel(QAbstractTableModel): def __init__(self, datain, parent=None): QAbstractTableModel.__init__(self, parent) self.arraydata = datain def rowCount(self, parent): return len(self.arraydata) def columnCount(self, parent): return len(self.arraydata[0]) def data(self, index, role): if not index.isValid(): return QVariant() elif role == Qt.EditRole: print( "edit mode" ) return None elif role != Qt.DisplayRole: return None return self.arraydata[index.row()][index.column()] if __name__ == "__main__" : app = QApplication( sys.argv ) myWindow = MyWindow() myWindow.show() sys.exit( app.exec_() ) |
Voici le résultat produit par cette application.
Améliorations / Corrections
Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Emplacement :
Description des améliorations :