转载:cw123458945
1 1 #!/usr/bin/env python 2 2 3 3 import sys 4 4 5 5 from PyQt4.QtCore import SIGNAL 6 6 7 7 from PyQt4.QtGui import QApplication 8 8 from PyQt4.QtGui import QMainWindow 9 9 from PyQt4.QtGui import QTreeWidget 1010 from PyQt4.QtGui import QTreeWidgetItem 1111 1212 1313 class MyTreeItem(QTreeWidgetItem): 1414 1515 def __init__(self, s, parent=None): 1616 1717 super(MyTreeItem, self).__init__(parent, [s]) 1818 1919 2020 class MyTree(QTreeWidget): 2121 2222 def __init__(self, parent=None): 2323 2424 super(MyTree, self).__init__(parent) 2525 self.setMinimumWidth(200) 2626 self.setMinimumHeight(200) 2727 for s in ['foo', 'bar']: 2828 MyTreeItem(s, self) 2929 self.connect(self, SIGNAL('itemClicked(QTreeWidgetItem*, int)'), self.onClick) 3030 3131 def onClick(self, item, column): 3232 3333 print(item.text(0)) 3434 3535 3636 class MainWindow(QMainWindow): 3737 3838 def __init__(self, parent = None): 3939 4040 super(MainWindow, self).__init__(parent) 4141 self.tree = MyTree(self) 4242 4343 4444 def main(): 4545 4646 app = QApplication(sys.argv) 4747 win = MainWindow() 4848 win.show() 4949 app.exec_() 5050 5151 5252 if __name__ == '__main__': 5353 main()