如何只编辑QTreeWidgetItem的一列

时间:2015-07-03 08:52:14

标签: python python-3.x pyqt pyqt5

我想知道如何只编辑QTreeWidgetItem的一列。

目前,我可以编辑QTreeWidgetItem的所有列。

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys

app = QApplication(sys.argv)
qApp = app

pointListBox = QTreeWidget()

header=QTreeWidgetItem(["Tree","First","secondo"])

pointListBox.setHeaderItem(header)   

root = QTreeWidgetItem(pointListBox, ["root"])
A = QTreeWidgetItem(root, ["A"])
barA = QTreeWidgetItem(A, ["bar", "i", "ii"])
bazA = QTreeWidgetItem(A, ["baz", "a", "b"])

barA.setFlags(barA.flags() | Qt.ItemIsEditable)
bazA.setFlags(bazA.flags() | Qt.ItemIsEditable)

pointListBox.show()
sys.exit(app.exec_())

修改

我添加了一些更改,但我不知道如何编辑QtreeWidgetItem的第二列。

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys

class MainWindow(QMainWindow):

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

        self.initUI()

    def createGroup(self):
         groupBox = QGroupBox()

        self.treeWidget = QTreeWidget()

        header=QTreeWidgetItem(["Tree","First","secondo"])
        #...
        self.treeWidget.setHeaderItem(header)   #Another alternative is setHeaderLabels(["Tree","First",...])

        root = QTreeWidgetItem(self.treeWidget, ["root"])
        A = QTreeWidgetItem(root, ["A"])
        barA = QTreeWidgetItem(A, ["bar", "i", "ii"])
        bazA = QTreeWidgetItem(A, ["baz", "a", "b"])

        barA.setFlags(barA.flags() | Qt.ItemIsEditable)
        bazA.setFlags(bazA.flags() | Qt.ItemIsEditable)

    # switch off "default" editing behaviour
    # as it does not allow to configure only an individual
    # column as editable
        self.treeWidget.setEditTriggers(self.treeWidget.NoEditTriggers)

    # to be able to decide on your own whether a particular item
    # can be edited, connect e.g. to itemDoubleClicked
        self.treeWidget.itemDoubleClicked.connect(self.checkEdit)


        vbox = QVBoxLayout()
        vbox.addWidget(self.treeWidget)

        vbox.addStretch(1)
        groupBox.setLayout(vbox)

        return groupBox

# in your connected slot, you can implement any edit-or-not-logic
# you want
    def checkEdit(self, item, column):
        # e.g. to allow editing only of column 1:
        if column == 1:
            self.treeWidget.editItem(item[1])

    def initUI(self):
        self.resize(300, 220)

        self.grid = QGridLayout()

        self.widget = QWidget()
        self.widget.setLayout(self.grid)
        self.setCentralWidget(self.widget)
        self.grid.addWidget(self.createGroup(),1,0,1,2)

        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    app.setStyle(QStyleFactory.create("Fusion"))
    form = MainWindow()
    form.show()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:1)

没有方法/标志可以直接配置它。

您可以通过完全关闭Qt' EditTriggers并实现自己的功能来决定是否应该编辑某个项目来解决这个问题:

class MyWidget(QWidget):

    def __init__(parent):

        self.treeWidget = ...

        # switch off "default" editing behaviour
        # as it does not allow to configure only an individual
        # column as editable
        self.treeWidget.setEditTriggers(self._treeWidget.NoEditTriggers)

        # to be able to decide on your own whether a particular item
        # can be edited, connect e.g. to itemDoubleClicked
        self.treeWidget.itemDoubleClicked.connect(self.checkEdit)

    # in your connected slot, you can implement any edit-or-not-logic
    # you want
    def checkEdit(self, item, column):
        # e.g. to allow editing only of column 1:
        if column == 1:
            self.treeWidget.editItem(item, column)

如果您还想在其他场合进行剪辑,只需将checkEdit连接到相应的信号,例如itemClickeditemEntered等。

答案 1 :(得分:0)

尝试一下,它对我有用。

self.treeWidget.itemDoubleClicked.connect(self.checkEdit)  
def checkEdit(self, item, column):

   tmp = item.flags()

   if column == 1 :
      item.setFlags(tmp | QtCore.Qt.ItemIsEditable)
   elif tmp & QtCore.Qt.ItemIsEditable:
      item.setFlags(tmp ^ QtCore.Qt.ItemIsEditable)