简体   繁体   English

PyQt5:如何使用 CheckBox 从 QTreeWidget 获取信号?

[英]PyQt5: How to get the signal from a QTreeWidget with CheckBox?

I am learning Pyqt5, but I can`t understand completely how to use signal and slot.我正在学习 Pyqt5,但我无法完全理解如何使用信号和插槽。

from PySide2.QtWidgets import QApplication, QTreeWidgetItem,QTreeWidget

from PySide2.QtCore import Qt

dat = { 'A':
            { 'A':
                {'1.1': ['1.1.1', '1.1.2'],
                 '1.2': ['1.2.1', '1.2.2']
                 },
             '2':
                {'2.1': ['2.1.1','2.1.2']}
            }
     }

def add(p,ch):
    if isinstance(ch,dict):
        for k,v in ch.items():
            item = QTreeWidgetItem(p)
            item.setText(0, k)
            item.setCheckState(0, Qt.Unchecked)
            item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
            add(item,v)

    else:
        for txt in ch:
            item = QTreeWidgetItem(p)
            item.setText(0, txt)
            item.setCheckState(0, Qt.Unchecked)
            item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)


app = QApplication([])
tw = QTreeWidget()

add(tw,dat)

app.show()
app.exec_()

I want to check the checkbox and return which node I selected but I don't know how to get the signal.我想选中复选框并返回我选择的节点,但我不知道如何获取信号。 I find that QTreeWidget has some signal func but i don't know how to use it.我发现 QTreeWidget 有一些信号功能,但我不知道如何使用它。 thank you for your help.感谢您的帮助。

You can connect the QTreeWidget.itemChanged signal to get the QTreeWidgetItem that was checked (or unchecked).您可以连接QTreeWidget.itemChanged信号以获取已选中(或未选中)的 QTreeWidgetItem。 I edited your code into a viewable GUI.我将您的代码编辑为可查看的 GUI。 This example will just print the text of the QTreeWidgetItem emitted by the signal.此示例将仅打印信号发出的 QTreeWidgetItem 的文本。

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Template(QWidget):

    def __init__(self):
        super().__init__()
        dat = { 'A':
                    { 'A':
                        {'1.1': ['1.1.1', '1.1.2'],
                         '1.2': ['1.2.1', '1.2.2']
                         },
                     '2':
                        {'2.1': ['2.1.1','2.1.2']}
                    }
             }
        tw = QTreeWidget()
        tw.itemChanged[QTreeWidgetItem, int].connect(self.get_item)
        grid = QGridLayout(self)
        grid.addWidget(tw)
        self.add(tw, dat)

    def get_item(self, item, column):
        if item.checkState(column) == Qt.Checked:
            print(f'{item.text(column)} was checked')
        else:
            print(f'{item.text(column)} was unchecked')

    def new_item(self, text):
        item = QTreeWidgetItem()
        item.setText(0, text)
        item.setCheckState(0, Qt.Unchecked)
        item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
        return item

    def add(self, p, ch):
        for k, v in ch.items():
            item = self.new_item(k)
            if isinstance(p, QTreeWidget):
                p.addTopLevelItem(item)
            else:
                p.addChild(item)

            if isinstance(v, dict):
                self.add(item, v)

            elif isinstance(v, list):
                for txt in v:
                    item.addChild(self.new_item(txt))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = Template()
    gui.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM