本文介绍了QTextEdit 作为 QTreeWidgetItem 的子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 QTreeWidget 中添加 QTextEdit 作为子项?

Is it possible to add a QTextEdit as a child in QTreeWidget?

这是我的代码,我们可以创建一个 QTreeWidget 并添加列:

Here is my code we can create a QTreeWidget and add the columns:

self.treetext = QtGui.QTreeWidget(self.dockWidgetContents_2)
        self.treetext.setObjectName(_fromUtf8("treetext"))
        self.verticalLayout_2.addWidget(self.treetext)
        self.treetext.setGeometry(QtCore.QRect(20, 10, 261, 241))
        item_0 = QtGui.QTreeWidgetItem(self.treetext)
        item_1 = QtGui.QTreeWidgetItem(item_0)
        item_1 = QtGui.QTreeWidgetItem(item_0)
        item_1 = QtGui.QTreeWidgetItem(item_0)
        item_1 = QtGui.QTreeWidgetItem(item_0)
        item_0 = QtGui.QTreeWidgetItem(self.treetext)
        item_1 = QtGui.QTreeWidgetItem(item_0)
        item_1 = QtGui.QTreeWidgetItem(item_0)
        item_1 = QtGui.QTreeWidgetItem(item_0)
        item_1 = QtGui.QTreeWidgetItem(item_0)

并将新项目添加为子项:

and add new items as child:

self.treetext.headerItem().setText(0, _translate("Form", "Model List", None))
        __sortingEnabled = self.treetext.isSortingEnabled()
        self.treetext.setSortingEnabled(False)
        self.treetext.topLevelItem(0).setText(0, _translate("Form", "Model 1", None))
        self.treetext.topLevelItem(0).child(0).setText(0, _translate("Form", "New Subitem", None))
        self.treetext.topLevelItem(0).child(1).setText(0, _translate("Form", "New Item", None))
        self.treetext.topLevelItem(0).child(2).setText(0, _translate("Form", "New Item", None))
        self.treetext.topLevelItem(0).child(3).setText(0, _translate("Form", "New Item", None))
        self.treetext.topLevelItem(1).setText(0, _translate("Form", "Model 2", None))
        self.treetext.topLevelItem(1).child(0).setText(0, _translate("Form", "New Subitem", None))
        self.treetext.topLevelItem(1).child(1).setText(0, _translate("Form", "New Item", None))
        self.treetext.topLevelItem(1).child(2).setText(0, _translate("Form", "New Item", None))
        self.treetext.topLevelItem(1).child(3).setText(0, _translate("Form", "New Item", None))
        self.treetext.setSortingEnabled(__sortingEnabled)

可以使用其他示例创建一个新的 QTextEdit:

Can Create a new QTextEdit with other example:

self.groupBox = QtGui.QTextEdit(self.dockWidgetContents_2)
self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.verticalLayout_2.addWidget(self.groupBox)

但是我们可以将 QTextEdit 作为 QTreeWidgetItem 的新子项吗?

But can we put QTextEdit as a new child for QTreeWidgetItem?

推荐答案

您可以使用 setItemWidget

self.treetext.setItemWidget(item_1, 0, QTextEdit(self))

如果您的树小部件项是可编辑的,您也可以只告诉 Qt 打开一个持久编辑器(默认情况下,QTreeWidgetItems 使用 QLineEdit 进行编辑,但您可以如果需要,可以使用 QItemDelegate 覆盖该行为)使用 openPersistentEditor()

If your tree widget items are editable, you can also just tell Qt to open a persistent editor (by default, QTreeWidgetItems use a QLineEdit for editing, but you can override that behavior with a QItemDelegate if you want) using openPersistentEditor()

self.treetext.openPersistentEditor(item_1, 0)

这篇关于QTextEdit 作为 QTreeWidgetItem 的子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 19:24