2016/7/20 1:18:29 PyQT5 炫酷的左侧导航效果

2016/7/20 1:18:29 
 
完整code
  1. from PyQt5.QtWidgets import (QApplication, QWidget, QHBoxLayout,
  2. QTreeWidget, QTreeWidgetItem,
  3. QGroupBox)
  4. from PyQt5.QtGui import QIcon, QPixmap
  5. from PyQt5.QtCore import QSize
  6. class Bar_Navigation(QWidget):
  7. def __init__(self):
  8. super(Bar_Navigation, self).__init__()
  9. self.setMinimumHeight(600);
  10. self.setMinimumWidth(800);
  11. ##layout_main.setMargin(5)
  12. ##layout_main.setSpace(5)
  13. self.creat_main_layout()
  14. self.creat_bar_navigation()
  15. self.creat_left_box()
  16. def get_bar_list(self):
  17. bar_list_item = []
  18. bar_list_1 = ["订货管理",
  19. ["客户订货", "库存订货", "供货商订货"]
  20. ]
  21. bar_list_2 = ["入库管理",
  22. ["采购入库", "订货入库", "其它入库"]
  23. ]
  24. bar_list_item.append(bar_list_1)
  25. bar_list_item.append(bar_list_2)
  26. return bar_list_item
  27. def creat_main_layout(self):
  28. self.layout_main = QHBoxLayout()
  29. self.setLayout( self.layout_main)
  30. def creat_bar_list(self, data):
  31. for item in data:
  32. item_1 = QTreeWidgetItem( self.tree_Navigation)
  33. ##item_1.setText(0,item[0])
  34. ## 设置节点的打开/关闭状态下的不同的图片
  35. icon = QIcon()
  36. ##节点打开状态
  37. icon.addPixmap(QPixmap("./logo2.png"), QIcon.Normal, QIcon.On)
  38. ##节点关闭状态  
  39. icon.addPixmap(QPixmap("./logo1.png"), QIcon.Normal, QIcon.Off)
  40. item_1.setIcon(0, icon)
  41. for item_item in item[1]:
  42. item_1_1 = QTreeWidgetItem( item_1);
  43. item_1_1.setText(0,item_item)
  44. self.tree_Navigation.addTopLevelItem(item_1);
  45. def creat_bar_navigation(self):
  46. self.tree_Navigation = QTreeWidget()
  47. self.tree_Navigation.setHeaderLabel("左侧导航栏");
  48. self.tree_Navigation.setColumnCount(1)
  49. self.tree_Navigation.setMaximumWidth(150);
  50. icon_size = QSize(100, 30)
  51. self.tree_Navigation.setIconSize(icon_size)
  52. ##如果treewidget就一列,该列的宽度默认等于treewidget的宽度,两列以上的话才起作用.
  53. ##self.tree_Navigation.setColumnWidth(0,100);
  54. data = self.get_bar_list()
  55. self.creat_bar_list(data)
  56. self.layout_main.addWidget(self.tree_Navigation)
  57. ## QModelIndex
  58. ##self.tree_Navigation.doubleClicked.connect(self.showModelSelected)
  59. ## QTreeWidgetItem
  60. self.tree_Navigation.itemDoubleClicked.connect(self.showSelected)
  61. ## QModelIndex
  62. def showModelSelected(self, modelIndex):
  63. print( modelIndex.row(), modelIndex.column())
  64. ## itemObj:QTreeWidgetItem
  65. def showSelected(self, item, column):
  66. ##获得父节点
  67. parent=item.parent()
  68. ## 注:本例的深度只有2,因此只有index_top,index_row两个变量表示路径
  69. ##根节点上的索引
  70. ## 合理值:非负整数
  71. index_top = 0
  72. ## 子节点上的索引
  73. ##如果为-1则标示是根节点上的项;如果不是-1则标示在子节点上
  74. ## 合理值:非负整数
  75. index_row = -1
  76. ## 如果是要判断是否为None,则:
  77. if parent is None:
  78. index_top= self.tree_Navigation.indexOfTopLevelItem(item)
  79. else :
  80. index_top = self.tree_Navigation.indexOfTopLevelItem(parent)
  81. index_row=parent.indexOfChild(item)##获得节点在父节点中的行号(从0开始)
  82. print(index_top, index_row)
  83. def creat_left_box(self):
  84. self.box_left = QGroupBox('left party')
  85. self.layout_main.addWidget(self.box_left)
  86. if __name__ == "__main__":
  87. import sys
  88. app = QApplication(sys.argv)
  89. window = Bar_Navigation()
  90. window.show()
  91. sys.exit(app.exec_())





posted @ 2016-07-20 12:33  乾坤颠倒  阅读(3369)  评论(0编辑  收藏  举报