QTextEdit as a child node for QTreeWidgetItem?

自闭症网瘾萝莉.ら 提交于 2019-12-25 07:28:26

问题


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)


来源:https://stackoverflow.com/questions/39839006/qtextedit-as-a-child-node-for-qtreewidgetitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!