How to make a cell in a QTableWidget read-only?

I have the following code defining the gui of my application

class Ui (object): def setupUi(): self.tableName = QtGui.QTableWidget(self.layoutWidget_20) self.tableName.setObjectName(_fromUtf8("twHistoricoDisciplinas")) self.tableName.setColumnCount(4) self.tableName.setRowCount(3) 

and the following code in my application

 class MainWindow(QtGui.QMainWindow): def __init__(self): self.ui = Ui() self.ui.setupUi(self) self.createtable() #creating a tw cell def cell(self,var=""): item = QtGui.QTableWidgetItem() item.setText(var) return item def createtable(self): rows = self.tableName.rowCount() columns = self.tableName.columnCount() for i in range(rows): for j in range(columns): item = self.cell("text") self.ui.tableName.setItem(i, j, item) 

I want to be able to add new rows and columns and edit them, but I want to block some of the cells. (I already have code expanding the table) how can I make some cells read-only while others are reading? I found this link. How to make a column in a QTableWidget read-only? with solving a problem in C ++, similar to python solution?

EDIT: Removed response from message and inserted as response

+4
source share
2 answers

I played a little with the code and read a few more documents to answer the question:

 def createtable(self): rows = self.tableName.rowCount() columns = self.tableName.columnCount() for i in range(rows): for j in range(columns): item = self.cell("text") # execute the line below to every item you need locked item.setFlags(QtCore.Qt.ItemIsEnabled) self.ui.tableName.setItem(i, j, item) 

The solution is the line "item.setFlags (QtCore.Qt.ItemIsEnabled)", you use it to disable the QtCore.Qt.ItemIsEnabled cell property, so you cannot select or edit the cell

You can change a number of other properties this way at runtime as described in the documentation at http://doc.qt.io/archives/qt-4.8/qt.html in the Qt :: ItemFlag section

as pointed out by Sven's comment on the second answer to this question, if you have a static number of rows and columns in QTableWidgetItem, you can select the cell properties with Qtdesigner if you use it to create screens for your application

+15
source

The edit status of a QTableWidgetItem is never entered if there are no edit triggers:

self.tableName.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)

+7
source

All Articles