Advertisement
pda0

non working qtableview delagate

Sep 16th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | Source Code | 0 0
  1. import sys
  2.  
  3. from PySide6.QtCore import QAbstractTableModel, QSize, Qt
  4. from PySide6.QtWidgets import QAbstractItemView, QApplication, QComboBox, QItemDelegate, QMainWindow, QStyle, QTableView
  5.  
  6. class ComboBoxDelegate(QItemDelegate):
  7.  
  8.     def __init__(self, parent):
  9.         super().__init__(parent)
  10.         self.items = ['AAA', 'BBB', 'CCC']
  11.  
  12.     def createEditor(self, parent, option, index):
  13.         editor = QComboBox(parent)
  14.         editor.addItems(self.items)
  15.         return editor
  16.  
  17.     def setEditorData(self, editor, index) -> None:
  18.         editor.setCurrentText(index.model().data(index, Qt.ItemDataRole.EditRole))
  19.  
  20.     def setModelData(self, editor, model, index):
  21.         model.setData(index, editor.currentText(), Qt.ItemDataRole.EditRole)
  22.  
  23.     def updateEditorGeometry(self, editor, option, index):
  24.         editor.setGeometry(option.rect)
  25.  
  26.     def paint(self, painter, option, index):
  27.         text = self.items[index.row()]
  28.         option.text = text
  29.         QApplication.style().drawControl(QStyle.CE_ItemViewItem, option, painter)
  30.  
  31.  
  32. class TableModel(QAbstractTableModel):
  33.  
  34.     def __init__(self):
  35.         super().__init__()
  36.  
  37.         self.__data = [['AAA', 1], ['BBB', 2]]
  38.  
  39.     def rowCount(self, index):
  40.         return len(self.__data)
  41.  
  42.     def columnCount(self, index):
  43.         return len(self.__data[0])
  44.  
  45.     def headerData(self, section, orientation, role):
  46.         if orientation == Qt.Horizontal and role == Qt.DisplayRole:
  47.             if section == 0:
  48.                 return 'str'
  49.             else:
  50.                 return 'int'
  51.  
  52.     def flags(self, index):
  53.         return Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEditable
  54.  
  55.     def data(self, index, role):
  56.         if index.isValid():
  57.             match role:
  58.                 case Qt.ItemDataRole.EditRole:
  59.                     return self.__data[index.row()][index.column()]
  60.                 case Qt.ItemDataRole.DisplayRole:
  61.                     return self.__data[index.row()][index.column()]
  62.  
  63.     def setData(self, index, value, role = Qt.EditRole) -> bool:
  64.         if index.isValid():
  65.             match role:
  66.                 case Qt.ItemDataRole.EditRole:
  67.                     self.__data[index.row()][index.column()] = value
  68.                     self.dataChanged.emit(index, index)
  69.                     return True
  70.  
  71.         return False
  72.  
  73.  
  74. app = QApplication(sys.argv)
  75.  
  76. window = QMainWindow()
  77. window.setFixedSize(QSize(800, 600))
  78.  
  79. w = QTableView(window)
  80. w.setFixedSize(QSize(700, 550))
  81. w.verticalHeader().hide()
  82. w.setSelectionBehavior(QAbstractItemView.SelectRows)
  83.  
  84. m = TableModel()
  85. w.setModel(m)
  86. w.selectRow(0)
  87.  
  88. d = ComboBoxDelegate(w)
  89. w.setItemDelegateForColumn(0, d)
  90.  
  91. window.setCentralWidget(w)
  92. window.show()
  93.  
  94. app.exec()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement