Set a whole column in `QTableWidget` read-only in python
pyside, python, qt, qt5
Solution
I got it working using `QLineEditor`
class QTableWidgetDisabledItem(QtGui.QItemDelegate):
"""
Create a readOnly QTableWidgetItem
"""
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
item = QtGui.QLineEdit(parent)
item.setReadOnly(True)
#item.setEnabled(False)
return item
def setEditorData(self, editor, index):
editor.blockSignals(True)
editor.setText(index.model().data(index))
editor.blockSignals(False)
def setModelData(self, editor, model, index):
model.setData(index, editor.text())
and then just simply use it as follow
self.Size = QTableWidgetDisabledItem(self.MyTable)
self.MyTable.setItemDelegateForColumn(4,self.Size)
Problem
i want to set a column in my table to read-only! i tried all possible combinations of flags without success ``` item = QtGui.QTableWidgetItem() from operator import xor item.setFlags(xor(item.flags(),QtCore.Qt.ItemIsEditable)) self.Table.setHorizontalHeaderItem(4, item) ``` i also tried `and not`, `!=` and `^` operators but the column items are still editable Update I think i was misunderstanding this! i thought when i set the HorizontalHeaderItem at a column to be not editable, that will make all new items in that column to be automatically not editable, when using the operations like `insertRow()` I had perform these function to each new added item after inserting new row! ``` tableWidget.insertRow(row+1) if tableWidget is self.myTable: item = QtGui.QTableWidgetItem() item.setFlags(item.flags() != QtCore.Qt.ItemIsEditable) tableWidget.setItem(row+1, 4, item) ``` I think a better (but more complex) solution is to use `setItemDelegateForColumn()` and `QtGui.QItemDelegate()` to create a read-only costum `QTableWidgetItem` which will be added each time a new row is inserted or created Edit Well i tried to use `setItemDelegateForColumn()` and `QtGui.QItemDelegate()` as mentioned above but i got the following Warning ``` > python main.py sys:1: RuntimeWarning: Invalid return value in function QItemDelegate.createEdit or, expected PySide.QtGui.QWidget, got PySide.QtGui.QTableWidgetItem. ``` My code for that was ``` class QTableWidgetDisabledItem(QtGui.QItemDelegate): """ """ def __init__(self, parent): QtGui.QItemDelegate.__init__(self, parent) def createEditor(self, parent, option, index): item = QtGui.QTableWidgetItem() item.setFlags(item.flags() != QtCore.Qt.ItemIsEditable) return item def setEditorData(self, editor, index): editor.blockSignals(True) editor.setData(index, editor.text()) editor.blockSignals(False) def setModelData(self, editor, model, index): model.setData(index, editor.text()) ``` and in the MainWindow ``` self.Size = QTableWidgetDisabledItem(self.MyTable) self.MyTable.setItemDelegateForColumn(4,self.Size) ``` It was good idea though ...