PyQt5基础学习-QTreeWidget().clicked.connect(为树控件添加响应事件) 1.QTreeWidget().currentItem(获得当前点击的树控件)

为树控件添加响应事件, 通过QTreeWidget().currentItem获得当前被点击的树控件的item

TreeEvent.py 

"""
为树节点添加响应事件
"""
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt

class TreeEvent(QMainWindow):

    def __init__(self):
        super(TreeEvent, self).__init__()
        self.resize(500, 300)
        self.setWindowTitle("为树节点添加响应事件")

        self.tree = QTreeWidget()

        self.tree.setColumnCount(2)

        self.tree.setHeaderLabels(["Key", "Value"])

        root = QTreeWidgetItem(self.tree)
        root.setText(0, 'root')
        root.setText(1, '0')

        child1 = QTreeWidgetItem(root)
        child1.setText(0, 'child1')
        child1.setText(1, '1')

        child2 = QTreeWidgetItem(root)
        child2.setText(0, 'child2')
        child2.setText(1, '2')

        child3 = QTreeWidgetItem(child2)
        child3.setText(0, 'child3')
        child3.setText(1, '3')

        self.tree.clicked.connect(self.onTreeClicked)
        self.setCentralWidget(self.tree)

    def onTreeClicked(self, index):
        item = self.tree.currentItem()
        print(index.row())
        print("key=%s, value=%s"%(item.text(0), item.text(1)))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = TreeEvent()
    main.show()

    sys.exit(app.exec_())

 

上一篇:MySql数据存储格式Compact及计算MySql的B+Tree高度


下一篇:编译器架构的LLVM IR 5st Day(分解struct)