简体   繁体   English

如何在 QTableWidget 中使特定单元格可编辑而其余单元格不可编辑?

[英]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).我有不可编辑的 QTableWidget。(我在创建 Ui 文件时设置了 noEditTriggers)。 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.我在 SO 和其他平台上查看了几个答案,但没有得到任何对我有用的东西。

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:@musicamante答案使用相同的基础是创建一个仅返回特定列中的一个编辑器的委托,优点是您不需要对 QTableWidget 进行子分类,并且该逻辑可以在其他类型的视图中使用:

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"如果您希望具有 NN 的单元格可编辑,那么您必须在满足该条件时返回编辑器: 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.更好的方法可能是覆盖edit()方法,并通过手动设置编辑触发器来执行默认实现(创建项目编辑器并开始编辑)。
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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Ctrl +单击使单元格可编辑(在创建具有不可编辑的单元格的表之后) - Make a cell editable (after creating the table with non-editable cells) with ctrl+click 在QTableWidget中,如何确定空单元格是否可编辑? - In a QTableWidget, how to determine if an empty cell is editable? (Django REST)覆盖(视图)`create` 方法中的不可编辑字段 - (Django REST) Override Non-Editable Field in the (Views) `create` Method 如何在 django 管理员中创建“仅创建”不可编辑字段 - How to make a “create-only” non-editable field in django admin 如何在模型类的Flask Admin视图中使字段不可编辑 - How to make a field non-editable in Flask Admin view of a model class Django:如何在内联模型表单集中默认情况下使字段不可编辑? - Django: How do I make fields non-editable by default in an inline model formset? 使用 PyQt 将不可编辑的 QComboBox 文本居中 - Center non-editable QComboBox Text with PyQt wxPython中不可编辑的文本框 - Non-editable text box in wxPython 有没有一种方法可以使Tkinter中的Text小部件不可点击和不可编辑? - Is there a way to make a Text widget in Tkinter non-clickable and non-editable? 如何在pygtk中使特定孩子的列可编辑 - How to make column editable for particular child in pygtk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM