Guest User

Untitled

a guest
Jan 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4.  
  5. from PyQt5.QtCore import *
  6. from PyQt5.QtWidgets import *
  7.  
  8.  
  9. class MyTableModel(QAbstractTableModel):
  10.  
  11. def __init__(self, parent: QObject=None):
  12.  
  13. super().__init__(parent)
  14.  
  15. self.entries = [
  16. ['row0, col0 (expanding)', 'row0, col1 (expanding)', 'row0, col2']
  17. , ['row1, col0 (expanding)', 'row1, col1 (expanding)', 'row1, col2']
  18. , ['row2, col0 (expanding)', 'row2, col1 (expanding)', 'row2, col2']
  19. ]
  20.  
  21. def headerData(
  22. self
  23. , section : int
  24. , orientation: Qt.Orientation
  25. , role : Qt.ItemDataRole=Qt.DisplayRole
  26. ):
  27. if orientation == Qt.Vertical:
  28. return QVariant()
  29.  
  30. if role == Qt.DisplayRole:
  31. if section == 0:
  32. return 'Expanding 0'
  33. if section == 1:
  34. return 'Expanding 1'
  35. if section == 2:
  36. return 'Fixed 0'
  37.  
  38. return 'Fix Your Columns'
  39.  
  40. def data(
  41. self
  42. , index: QModelIndex=QModelIndex()
  43. , role : Qt.ItemDataRole=Qt.DisplayRole
  44. ):
  45. if role != Qt.DisplayRole:
  46. return QVariant()
  47.  
  48. return self.entries[index.row()][index.column()]
  49.  
  50. def rowCount(self, parent: QModelIndex=QModelIndex()):
  51. return len(self.entries)
  52.  
  53. def columnCount(self, parent: QModelIndex=QModelIndex()):
  54. return len(self.entries[0])
  55.  
  56.  
  57. class MyTableView(QTableView):
  58.  
  59. def __init__(self, parent: QObject=None):
  60. super().__init__(parent)
  61.  
  62.  
  63. class MyHeaderView(QHeaderView):
  64.  
  65. def __init__(
  66. self
  67. , orientation: Qt.Orientation=Qt.Horizontal
  68. , parent : QWidget=None
  69. ):
  70. super().__init__(orientation, parent)
  71.  
  72. Stretch, Fixed = QHeaderView.Stretch, QHeaderView.Fixed
  73. self.SECTION_RESIZE_MODES = [Stretch, Stretch, Fixed]
  74.  
  75. def column(self) -> int:
  76. return 3
  77.  
  78. def setModel(self, model: QAbstractItemModel=None):
  79.  
  80. super().setModel(model)
  81.  
  82. for i, mode in enumerate(self.SECTION_RESIZE_MODES):
  83. print(i, mode)
  84. self.setSectionResizeMode(i, mode)
  85.  
  86.  
  87. class MyCentralWidget(QWidget):
  88.  
  89. def __init__(self, parent: QObject=None):
  90.  
  91. super().__init__(parent)
  92.  
  93. self.my_table_model = MyTableModel(parent=parent)
  94.  
  95. self.my_table_view = MyTableView(parent=parent)
  96. self.my_header_view = MyHeaderView(parent=parent)
  97.  
  98. self.my_table_view.setModel(self.my_table_model)
  99. self.my_header_view.setModel(self.my_table_model)
  100.  
  101. self.my_table_view.setHorizontalHeader(self.my_header_view)
  102.  
  103. self.layout = QVBoxLayout(self)
  104. self.layout.addWidget(self.my_table_view)
  105.  
  106. self.setLayout(self.layout)
  107.  
  108.  
  109. class MyMainWindow(QMainWindow):
  110.  
  111. def __init__(self, parent: QObject=None):
  112.  
  113. super().__init__(parent)
  114.  
  115. self.central_widget = MCentralWidget(self)
  116.  
  117. self.setCentralWidget(self.central_widget)
  118.  
  119.  
  120. if __name__ == '__main__':
  121.  
  122. app = QApplication(sys.argv)
  123.  
  124. main_window = MyMainWindow()
  125. main_window.show()
  126.  
  127. sys.exit(app.exec())
  128.  
  129. self.my_table_view = MyTableView(parent=parent)
  130. self.my_header_view = MyHeaderView(parent=parent)
  131.  
  132. self.my_table_view.setHorizontalHeader(self.my_header_view)
  133.  
  134. self.my_table_view.setModel(self.my_table_model)
  135. self.my_header_view.setModel(self.my_table_model)
Add Comment
Please, Sign In to add comment