python pyqt5 QTreeWidget 点击事件

image.png

!/usr/bin/env python3

''' 【简介】 PyQT5中 QTreeWidget 例子

'''

from PyQt5.QtWidgets import * import sys

class TreeWidgetDemo(QMainWindow): def init(self, parent=None): super(TreeWidgetDemo, self).init(parent) self.setWindowTitle('TreeWidget 例子') 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(root)
    child3.setText(0, 'child3')
    child3.setText(1, '3')

    child4 = QTreeWidgetItem(child3)
    child4.setText(0, 'child4')
    child4.setText(1, '4')

    child5 = QTreeWidgetItem(child3)
    child5.setText(0, 'child5')
    child5.setText(1, '5')

    self.tree.addTopLevelItem(root)
    self.tree.clicked.connect(self.onTreeClicked)

    self.setCentralWidget(self.tree)

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

if name == 'main': app = QApplication(sys.argv) tree = TreeWidgetDemo() tree.show() sys.exit(app.exec_())

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券