将QLineEdit()放入QTreeWidgetItem()

时间:2015-06-22 16:23:50

标签: python python-3.x pyqt pyqt5

是否可以将QLineEdit()放入QTreeWidgetItem()以修改QTreeWidgetItem的文本?

这是我的代码

def addItemsToTree(self, parent, text, checkable=False, expanded=True):
    self.item = QTreeWidgetItem(parent, [text])
    if checkable:
        self.item.setCheckState(0, Qt.Unchecked)
    else:
        self.item.setFlags(self.item.flags() & ~Qt.ItemIsUserCheckable)
    self.item.setExpanded(expanded)
    min = QLineEdit()
    max = QLineEdit()
    self.addChildTree(self.item, self.column, "Min =", "Min =")
    self.addChildTree(self.item, self.column, "Max =", "Max =")
    return self.item

def addChildTree(self, parent, column, title, data):
    item = QTreeWidgetItem(parent, [title])
    item.setData(column, Qt.UserRole, data)
    return item

1 个答案:

答案 0 :(得分:3)

将项目标记设置为包含ItemIsEditable

就足够了
self.item.setFlags(self.item.flags() | Qt.ItemIsEditable)

您还可以将EditTriggers配置为根据需要开始编辑,例如:双击项目时:

treeView.setEditTriggers(QtGui.QAbstractItemView.DoubleClicked)

双击树形图表中的项目现在应该打开一个编辑器 - 默认情况下只是QLineEdit