Disable editing of QTableWidget in PyQt5 (with scrolling enabled)

Total
1
Shares

I need to disable the editing and resizing of my QTableWidget while keeping the scrolling enabled. Also, the user should be able to select and copy data from the table.

First I tried this

self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)

But the user is able to drag and resize the width of columns. I don’t want that. So I tried this –

self.tableWidget.setDisabled(1)

Now this prevents the user from resizing the table but it also disables the scrolling and the selection of cell data.

How can I achieve both functionalites? (actually all three – no editing, no resizing, and scrolling enabled)


Solution

To disable editing your solution is correct:

self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)

and if you want that the width of the items is not allowed to be changed, you must do it through the header:

self.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
Leave a Reply

Your email address will not be published. Required fields are marked *