QTreeWidget、QTreeView
QTreeWidget基本用法
''' 树控件(QTreeWidget)的基本用法 ''' import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import QIcon, QBrush, QColor from PyQt5.QtCore import Qt class BasicTreeWidget(QMainWindow): def __init__(self, parent=None): super(BasicTreeWidget, self).__init__(parent) self.setWindowTitle('树控件(QTreeWidget)的基本用法') self.tree = QTreeWidget() # 为树控件指定列数 self.tree.setColumnCount(2) # 指定列标签 self.tree.setHeaderLabels(['Key','Value']) root = QTreeWidgetItem(self.tree) root.setText(0,'根节点') root.setIcon(0,QIcon('./images/root.png')) self.tree.setColumnWidth(0,160) # 添加子节点1 child1 = QTreeWidgetItem(root) child1.setText(0,'子节点1') child1.setText(1,'子节点1的数据') child1.setIcon(0,QIcon('./images/bao3.png')) child1.setCheckState(0,Qt.Checked) # 添加子节点2 child2 = QTreeWidgetItem(root) child2.setText(0,'子节点2') child2.setIcon(0,QIcon('./images/bao6.png')) # 为child2添加一个子节点 child3 = QTreeWidgetItem(child2) child3.setText(0,'子节点2-1') child3.setText(1,'新的值') child3.setIcon(0,QIcon('./images/music.png')) self.tree.expandAll() self.setCentralWidget(self.tree) if __name__ == '__main__': app = QApplication(sys.argv) tree = BasicTreeWidget() tree.show() sys.exit(app.exec_())
QTreeWidget树节点添加事件
''' 为树节点添加响应事件 ''' from PyQt5.QtWidgets import * from PyQt5.QtCore import * import sys class TreeEvent(QMainWindow): def __init__(self, parent=None): super(TreeEvent, self).__init__(parent) 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) tree = TreeEvent() tree.show() sys.exit(app.exec_())
QTreeWidget添加节点、编辑节点、删除节点
''' 添加、修改和删除树控件中的节点 ''' import sys from PyQt5.QtWidgets import * class ModifyTree(QWidget): def __init__(self, parent=None): super(ModifyTree, self).__init__(parent) self.setWindowTitle('TreeWidget 例子') operatorLayout = QHBoxLayout() addBtn = QPushButton('添加节点') updateBtn = QPushButton('修改节点') deleteBtn = QPushButton('删除节点') operatorLayout.addWidget(addBtn) operatorLayout.addWidget(updateBtn) operatorLayout.addWidget(deleteBtn) addBtn.clicked.connect(self.addNode) updateBtn.clicked.connect(self.updateNode) deleteBtn.clicked.connect(self.deleteNode) 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) mainLayout = QVBoxLayout(self) mainLayout.addLayout(operatorLayout) mainLayout.addWidget(self.tree) self.setLayout(mainLayout) def onTreeClicked(self,index): item = self.tree.currentItem() print(index.row()) print('key=%s,value=%s' % (item.text(0),item.text(1))) # 添加节点 def addNode(self): print('添加节点') item = self.tree.currentItem() print(item) node = QTreeWidgetItem(item) node.setText(0,'新节点') node.setText(1,'新值') def updateNode(self): print('修改节点') item = self.tree.currentItem() item.setText(0,'修改节点') item.setText(1, '值已经被修改') def deleteNode(self): print('删除节点') item = self.tree.currentItem() root = self.tree.invisibleRootItem() for item in self.tree.selectedItems(): (item.parent() or root).removeChild(item) if __name__ == '__main__': app = QApplication(sys.argv) tree = ModifyTree() tree.show() sys.exit(app.exec_())
QTreeView
''' QTreeView与系统定制模式 QTreeWidget Model QDirModel ''' import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * if __name__=="__main__": app=QApplication(sys.argv) model=QDirModel() tree=QTreeView() tree.setModel(model) tree.setWindowTitle("QtreeView") tree.resize(600,400) tree.show() sys.exit(app.exec())
天道酬勤 循序渐进 技压群雄