QTextEdit as a child node for QTreeWidgetItem?

2019-09-04 07:00发布

问题:

Is it possible to add a QTextEdit as a child in 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)

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)

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

回答1:

You can set a widget on any item in the tree using setItemWidget

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

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)