cmaureir

Untitled

Jun 18th, 2021
773
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import sys
  3.  
  4. from PySide2.QtGui import *
  5. from PySide2.QtCore import *
  6. from PySide2.QtWidgets import *
  7.  
  8. class RestrictedHeaderView(QHeaderView):
  9.     def __init__(self, cols, parent=None):
  10.         super().__init__(Qt.Horizontal, parent)
  11.         self.visibleColumns = cols
  12.     def sectionsInserted(self, parent, logicalFirst, logicalLast):
  13.         if not parent.isValid() and logicalLast >= self.visibleColumns:
  14.             for col in range(visibleColumns, logicalLast + 1):
  15.                 self.hideSection(col)
  16.  
  17.  
  18. class MainWindow(QMainWindow):
  19.     def __init__(self):
  20.         super(MainWindow, self).__init__()
  21.  
  22.         # Init TableView
  23.         self.table = QTableView()
  24.         my_headers = ["H-1", "H-2", "H-3"]
  25.         my_col_ct = len(my_headers)
  26.         self.mdl_table = QStandardItemModel(0, my_col_ct) # 0 rows, n columns
  27.         self.table.setModel(self.mdl_table)
  28.         header = RestrictedHeaderView(5, parent=self)
  29.         self.table.setHorizontalHeader(header)
  30.  
  31. def main():
  32.     qt_app = QApplication(sys.argv)
  33.     my_main_window = MainWindow()
  34.     my_main_window.show()
  35.     sys.exit(qt_app.exec_())
  36.  
  37.  
  38. if __name__ == '__main__':
  39.     main()
  40.  
RAW Paste Data