简体   繁体   中英

How to make particular cell editable and leave the rest non-editable in QTableWidget?

I have QTableWidget which is non editable.(i had setup noEditTriggers while creating Ui file). I want to make particular cell editable from each row. how i can get this done?

I looked into several answers on SO and other platforms but didn't get anything working for me.

currently I am using this piece of code. it doesnt give an error but i still could not edit that cell value.

self.item = QTableWidgetItem('Hi')
flags = self.item.flags()
flags ^= QtCore.Qt.ItemIsEditable
self.item.setFlags(flags)
self.table.setItem(row, column, self.item)

EDIT::

获取表格视图的代码段

Using the same fundament for the @musicamante answer is to create a delegate that only returns one editor in the specific column, the advantage is that you don't need to subclassify QTableWidget and the logic can be used in other types of views:

class Delegate(QtWidgets.QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        if index.column() == 2:
            return super(Delegate, self).createEditor(parent, option, index)
delegate = Delegate(self.table)
self.table.setItemDelegate(delegate)

Update:

If you want the cells with NN to be editable then you must return the editor when it meets that condition: index.data() == "NN"

import random
import sys

from PyQt5 import QtWidgets


class Delegate(QtWidgets.QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        if index.data() == "NN":
            return super(Delegate, self).createEditor(parent, option, index)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    texts = ["Hello", "Stack", "Overflow", "NN"]

    table = QtWidgets.QTableWidget(10, 5)
    delegate = Delegate(table)
    table.setItemDelegate(delegate)

    for i in range(table.rowCount()):
        for j in range(table.columnCount()):
            text = random.choice(texts)
            it = QtWidgets.QTableWidgetItem(text)
            table.setItem(i, j, it)

    table.resize(640, 480)
    table.show()
    sys.exit(app.exec_())

You could set the flags for each item, while leaving the default edit triggers, but this is not very good approach, since you could have a very large table, some items could be changed/added/removed and you might forget to set/reset the flags.

A better approach could be to override the edit() method, and execute the default implementation (which creates the item editor and starts the editing) by manually setting the edit trigger.
This requires to leave the default edit triggers (or at least one trigger method) set.

class TableWidget(QtWidgets.QTableWidget):
    def edit(self, index, trigger, event):
        # editing is allowed only for the third column
        if index.column() != 2:
            trigger = self.NoEditTriggers
        return super().edit(index, trigger, event)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM