单个单元格的可编辑

单个单元格的可编辑

本文介绍了更改 QTableWidget 单个单元格的可编辑性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 QTableWidget 作为用户的 I/O 功能,但我希望通过禁用仅用于数据输出的单元格的可编辑性使其更加用户友好和直观.

I use a QTableWidget as an I/O feature for the user but I want to make it more user friendly and intuitive by disabling the editability of cells which are only for data output.

我可以取消选中 Qt Designer 中每个单元格标志下的可编辑",但是一旦我使用 self.table_item.setItem(row, column, QTableWidgetItem(str(value)) 更改单元格的值) 单元格可以再次编辑.

I can uncheck "Editable" under flags for each individual cell in Qt Designer but as soon as I change the value of a cell with self.table_item.setItem(row, column, QTableWidgetItem(str(value))) the cells is editable again.

这个问题/答案 建议使用 self.table_item.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers),但这会改变整个 QTableWidget 项目的标志,而不仅仅是一个单元格.

This question/answer suggest using self.table_item.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers), but that changes the flag for the whole QTableWidget item and not only one cell.

我也试过 self.table_time.item(row,column).setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers) 但我得到了回溯 'QTableWidgetItem' object has no attribute 'setEditTriggers'.

I've also tried self.table_time.item(row,column).setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers) but I get the traceback 'QTableWidgetItem' object has no attribute 'setEditTriggers'.

.setFlags() 似乎是我确实需要使用的功能,但我不知道如何使用它来使某些内容不可编辑或如何将其应用于单个单元格.

问题:在通过 .setItem() 更改单元格 QTableWidgetItem 的值后,如何更改 PyQt5 中 QTableWidget 的单个单元格的可编辑"标志.

Question:How can I change the "editable" flag for a individual cell of a QTableWidget in PyQt5 after changing the value of the cells QTableWidgetItem trough .setItem().

如果您提供额外的解决方案,以优雅的方式使整行不可编辑,您将获得额外的虚拟互联网点.

You will get extra imaginary internet points if you provide an additional solution to make a whole row un-editable in an elegant way.

推荐答案

您必须禁用标志 Qt::ItemIsEditable:

from PyQt5 import QtCore, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        table_widget = QtWidgets.QTableWidget(4, 3)
        self.setCentralWidget(table_widget)

        it = QtWidgets.QTableWidgetItem("not editable")
        it.setFlags(it.flags() & ~QtCore.Qt.ItemIsEditable)
        table_widget.setItem(1, 1, it)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.resize(320, 240)
    w.show()
    sys.exit(app.exec_())

如果您想优雅地禁用行或列,您可以使用 createEditor 方法返回 None 的委托:

If you want to elegantly disable a row or column you can use a delegate where the createEditor method returns None:

from PyQt5 import QtCore, QtWidgets


class ReadOnlyDelegate(QtWidgets.QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        return


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        table_widget = QtWidgets.QTableWidget(4, 3)
        self.setCentralWidget(table_widget)

        delegate = ReadOnlyDelegate(table_widget)
        table_widget.setItemDelegateForRow(1, delegate)
        # table_widget.setItemDelegateForColumn(1, delegate)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.resize(320, 240)
    w.show()
    sys.exit(app.exec_())

这篇关于更改 QTableWidget 单个单元格的可编辑性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:40