Recommended approach for cell validation on dynamic table.
The scenario: I have a QDialog where based on different dropdown selections 1 or more tables are dynamically added. Because tables are dynamically added, the standard cell clicked signal is not enough. It only provides the row and column, and I need to know which table was clicked in addition to the row and column. More specifically, I have 2 columns with integer values. When a cell is changed in one of the columns, they must be within a valid range, and the value of the cell in the 2nd column must be >= value of the cell in the first column.
I'm fairly new to Python, but my thinking is that I need to create a class that extends the QTableWidgetItem with the additional information I need and sends a custom signal, which I can then wire up to a slot within the dialog. I've tried several variations of the following code, but can't get things quite right:
class SmartCell(QtCore.QObject):
valueChanged = QtCore.pyqtSignal(str) # Signal to be emitted when value changes.
def __init__(self, tbl, rowname, colname, value):
QtGui.QTableWidgetItem.__init__(self)
self.tbl_name = tbl
self.row_name = rowname
self.col_name = colname
# self.setText(value)
self.__value = value
@property
def value(self):
return self.__value
@value.setter
def value(self, value):
if self.__value != value:
self.__value = value
# self.setText(value)
signal = self.tbl_name + ":" + self.row_name + ":" + self.col_name + ":" + self.text()
self.valueChanged.emit(signal)
and then in the dialog, after importing the SmartCell reference as sCell:
item = sCell(obj_name, f.part_name, "start_frame", str(f.start_frame))
item.valueChanged.connect(self.frame_cell_changed)
tbl.setItem(rowcounter, 1, item)
item = sCell(obj_name, f.part_name, "end_frame", str(f.end_frame))
item.valueChanged.connect(self.frame_cell_changed)
tbl.setItem(rowcounter, 2, item)