From pyqt5.qtwidgets import (Qapplication, Qwidget, Qhboxlayout, Qtreewidget, Qtreewidgetit EM, Qgroupbox) from Pyqt5.qtgui import Qicon, qpixmapfrom pyqt5.qtcore import Qsizeclass Ba R_navigation (qwidget): def __init__ (self): Super (Bar_navigation, self). __init__ () SELF.SETMI Nimumheight (600); Self.setminimumwidth (800); # #layout_main. SetMargin (5) # #layout_main. Setspace (5) self.creat_main_layout () Self.creat_ Bar_navigation () Self.creat_left_box () def get_bar_list (self): bar_list_i TEM = [] bar_list_1 = ["Order Management", ["Customer Order", "Inventory Order", "Supplier Order"] ] Bar_list_2 = ["Inbound Management", ["Purchase Warehousing", "Order warehousing", " Other storage "]] Bar_list_iteM.append (bar_list_1) bar_list_item.append (bar_list_2) return Bar_list_item def C Reat_main_layout (self): Self.layout_main = Qhboxlayout () self.setlayout (Self.layout_main) def creat_bar_list (self, data): for item in data:item_1 = Qtreewidgetitem (self.tree_navigation); # #item_1. SetText (0,item[0])
# # Set the different pictures in the open/closed state of the node icon = Qicon () # #节点打开状态 Icon.addpixmap (Qpix Map ("./logo2.png"), Qicon.normal, Qicon.on) # #节点关闭状态 Icon.addpixmap (Qpixmap ("./logo1.png" ), Qicon.normal, Qicon.off) item_1.seticon (0, icon) for Item_item in item[1] : Item_1_1 = Qtreewidgetitem (item_1); Item_1_1.settext (0,item_item) Self.tree_Navigation.addTopLevelItem (item_1); def creat_bar_navigation (self): Self.tree_navigation = Qtreewidget () Self.tree_Navigation.setHeaderLabel ("left navigation bar"); Self.tree_Navigation.setColumnCount (1) self.tree_Navigation.setMaximumWidth (150); Icon_size = qsize (+) self.tree_Navigation.setIconSize (icon_size) # #如果treeThe widget is a column with a width that defaults to the width of treewidget, and more than two columns will work. # #self. Tree_navigation.setcolumnwidth (0,100); data = Self.get_bar_list () self.creat_bar_list (data) Self.layout_main.addWidget (self.tree_navigation # # # Qmodelindex # #self. Tree_Navigation.doubleClicked.connect (self.showmodelselected) # # Qtreewidgetitem Self.tree_Navigation.itemDoubleClicked.connect (self.showselected) # # Qmodelindex def showmodelselected (self, Modelindex): Print (Modelindex.row (), Modelindex.column ()) # # Itemobj:qtreewidgetitem def showselected (self, Item, column): # #获得父节点 PARENT=ITEM.PA rent () # # Note: This example has a depth of only 2, so only index_top,index_row two variables represent path # #根节点上的索引 # # Reasonable value: Non-negative integer Number index_top = 0 # # The index on the child node # #如果为-1 is the item on the root node, and if not-1 is indicated on the child node # # Reasonable value: nonnegative integer Index_row =-1 # # If you want to determine if it is None, then: If Parent is none:index_top= Self.tree_Navigation.indexOfTopLevel Item (item) Else:index_top = Self.tree_Navigation.indexOfTopLevelItem (parent) Index_row=parent.indexofchild (item) # #获得节点在父节点中的行号 (starting from 0) print (index_top, Inde X_row) def creat_left_box (self): Self.box_left = Qgroupbox ("Left Party") self. Layout_main.addwidget (self.box_left) If __name__ = = "__main__": Import sys app = qapplication (sys.argv) window = B Ar_navigation () window.show () Sys.exit (App.exec_ ())
Pyqt5 Qtreewidget Gets the hierarchical path of the child that was double-clicked