2

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
stepBystep
  • 187
  • 1
  • 1
  • 8
  • what is the meaning of: "I don't want to put the same code for every table"? – eyllanesc Apr 20 '17 at 08:42
  • for example: `self.Size = QTableWidgetEnDisabledItem(self.general_table, "enabled") self.general_table.setItemDelegateForRow(index.row(),self.Size) self.Size = QTableWidgetEnDisabledItem(self.layers_table, "enabled") self.layers_table.setItemDelegateForRow(index.row(),self.Size) ` – stepBystep Apr 20 '17 at 08:45

1 Answers1

2

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) 
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • it's work perfect and it's very helpful. I have thought that I have to change the class parameter as well if I work with list. But nice idea ant tkank you. – stepBystep Apr 20 '17 at 09:00