如何只编辑 QTreeWidgetItem 的一列
How to edit only one column of a QTreeWidgetItem
我想知道如何只编辑 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_())
没有方法/标志可以直接配置它。
您可以通过完全关闭 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
连接到相应的信号,例如 itemClicked
、itemEntered
等
试试这个,它对我有用。
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)
我想知道如何只编辑 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_())
没有方法/标志可以直接配置它。
您可以通过完全关闭 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
连接到相应的信号,例如 itemClicked
、itemEntered
等
试试这个,它对我有用。
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)