Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pathlib
- from PySide2 import QtWidgets, QtCore
- class ComboBoxWithRC(QtWidgets.QComboBox):
- RightClicked = QtCore.Signal() #emit this to edit multiple selections via RC
- def mousePressEvent(self, event):
- if event.button() == QtCore.Qt.MouseButton.RightButton:
- self.RightClicked.emit()
- else:
- super(ComboBoxWithRC, self).mousePressEvent(event)
- class TypeDelegate(QtWidgets.QStyledItemDelegate):
- EditNow = QtCore.Signal() #emit this to edit multiple selections via RC
- def __init__(self, parent=None):
- super(TypeDelegate, self).__init__(parent)
- self.types = ["A", "B", "C", "D", "E"]
- def createEditor(self, parent, option, index):
- w = ComboBoxWithRC(parent)
- w.RightClicked.connect(self.EditNow.emit)
- w.addItems(self.types)
- return w
- def setModelData(self, editor, model, index):
- model.setData(index, editor.currentText())
- class PublishTable(QtWidgets.QTableWidget):
- HEADERS = ["File Path", "Type"]
- def __init__(self, parent=None):
- super(PublishTable, self).__init__(parent)
- self.setColumnCount(len(PublishTable.HEADERS))
- self.setHorizontalHeaderLabels(PublishTable.HEADERS)
- self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectionBehavior.SelectRows)
- self.type_delegate = TypeDelegate()
- self.type_delegate.EditNow.connect(self.edit_selection)
- self.setItemDelegateForColumn(PublishTable.HEADERS.index("Type"), self.type_delegate)
- def load_data(self, path_list):
- prev_row_count = self.rowCount()
- # add the required rows:
- self.setRowCount(prev_row_count + len(path_list))
- data_list = []
- for path in path_list:
- data = (path, None, None)
- data_list.append(data)
- # add the new data
- for row, row_data in enumerate(data_list):
- new_row_index = row + prev_row_count
- for column, value in enumerate(row_data):
- item = QtWidgets.QTableWidgetItem(value)
- self.setItem(new_row_index, column, item)
- self.openPersistentEditor(self.model().index(new_row_index, PublishTable.HEADERS.index("Type")))
- def edit_selection(self):
- print("editing multipled rows")
- print(self.selectedIndexes())
- if __name__ == "__main__":
- import sys
- args = sys.argv
- app = QtWidgets.QApplication(args)
- w = PublishTable()
- w.load_data(["/path/1", "/path/2", "/path/3"])
- w.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement