fork download
  1. from PyQt6.QtWidgets import QMainWindow, QTableView, QApplication
  2. from PyQt6.QtCore import QAbstractTableModel, QModelIndex, Qt
  3. import typing
  4.  
  5.  
  6. class Model(QAbstractTableModel):
  7.  
  8. def __init__(self):
  9. super().__init__()
  10. self._data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  11.  
  12. def data(self, index: QModelIndex, role: int = ...) -> typing.Any:
  13.  
  14. if role == Qt.ItemDataRole.DisplayRole:
  15. return self._data[index.row()][index.column()]
  16.  
  17. elif role == Qt.ItemDataRole.TextAlignmentRole:
  18. return Qt.AlignmentFlag.AlignCenter
  19.  
  20. def setData(self, index: QModelIndex, value: typing.Any, role: int = ...) -> bool:
  21. self._data[index.row()][index.column()] = value
  22.  
  23. def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...) -> typing.Any:
  24.  
  25. if role == Qt.ItemDataRole.DisplayRole:
  26. return section
  27.  
  28. elif role == Qt.ItemDataRole.TextAlignmentRole:
  29. return Qt.AlignmentFlag.AlignCenter
  30.  
  31. def columnCount(self, parent: QModelIndex = ...) -> int:
  32. return len(self._data[0])
  33.  
  34. def rowCount(self, parent: QModelIndex = ...) -> int:
  35. return len(self._data)
  36.  
  37.  
  38. class MainWindow(QMainWindow):
  39.  
  40. def __init__(self):
  41. super().__init__()
  42. self.model = Model()
  43.  
  44. self.table = QTableView()
  45. self.table.setModel(self.model)
  46.  
  47. self.setCentralWidget(self.table)
  48.  
  49.  
  50. app = QApplication([])
  51.  
  52. window = MainWindow()
  53. window.show()
  54.  
  55. app.exec()
  56. # your code goes here
Runtime error #stdin #stdout #stderr 0.15s 23220KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
ModuleNotFoundError: No module named 'PyQt6'