Search code examples
pythonpyqtpyqt4qtablewidgetqtablewidgetitem

ReadOnly row for more than one QTableWidget with class and get more efficiency


How could I rewrite that code bellow for more then one QTableWidget(not only the self.general_table) ? I don't want to put the same code for every table.

class QTableWidgetEnDisabledItem(QtGui.QItemDelegate):
    """
    Create a readOnly/editable QTableWidgetItem
    """
    def __init__(self, parent, state):
        self.state = state
        QtGui.QItemDelegate.__init__(self, parent)

    def createEditor(self, parent, option, index):
        item = QtGui.QLineEdit(parent)
        if self.state == "disabled":
            item.setReadOnly(True)
        elif self.state == "enabled":
            item.setEnabled(True)
        return item

Execution of the class QTableWidgetEnDisabledItem

self.Size = QTableWidgetEnDisabledItem(self.general_table, "enabled")
self.general_table.setItemDelegateForRow(index.row(),self.Size)

Solution

  • You could create a list with the tables that you have to use a for to execute the commands that you want.

    tables = [self.general_table, self.layers_table]
    
    for table in tables:
        itemDelegate = QTableWidgetEnDisabledItem(table, "enabled") 
        table.setItemDelegateForRow(index.row(), itemDelegate)