Вопрос

My question is: I have a QListWidget with items(on the left side of window) and QTextEdit on the right

items               textEdit 
-item1
---subitem1
---subitem2
-item2
---subitem1
---subitem2

I fill the listWidget like this:

for name in names:
        item = QtWidgets.QTreeWidgetItem([name])
        self.treeWidget.addTopLevelItem(item)
        for cmd in description:
            item2 = QtWidgets.QTreeWidgetItem([cmd])
            item.addChild(item2)

Then i want that if i cklicked on subitems some text appears in textEdit Only subitems can appear text. I wrote

self.treeWidget.itemClicked.connect(self.item2Clicked)
....
def item2Clicked(self, item, column):
    self.textEdit.insertPlainText("hello")

But it's not right, because if i click on the item1 - text appears too. Thanks for your help and sorry for my english:)

Это было полезно?

Решение

Top-level items don't have a parent, so you could do:

    def item2Clicked(self, item, column):
        if item.parent() is not None:
            self.textEdit.insertPlainText("hello")

More generally, you can set a type for each item in the constructor:

    item = QtWidgets.QTreeWidgetItem([name], 1)
    self.treeWidget.addTopLevelItem(item)
    for cmd in description:
        item2 = QtWidgets.QTreeWidgetItem([cmd], 2)

and then do:

    def item2Clicked(self, item, column):
        if item.type() == 2:
            self.textEdit.insertPlainText("hello")
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top