| 92 | |
| 93 | |
| 94 | class DragGraphics(QWidget): |
| 95 | |
| 96 | def __init__(self, *args, **kwargs): |
| 97 | super(DragGraphics, self).__init__(*args, **kwargs) |
| 98 | self.resize(800, 600) |
| 99 | layout = QHBoxLayout(self) |
| 100 | |
| 101 | # 左侧树形控制 |
| 102 | self.treeWidget = QTreeWidget(self) |
| 103 | self.treeWidget.header().setVisible(False) |
| 104 | self.treeWidget.setMaximumWidth(300) |
| 105 | layout.addWidget(self.treeWidget) |
| 106 | |
| 107 | # 右侧图形显示 |
| 108 | self.graphicsView = GraphicsView(self) |
| 109 | layout.addWidget(self.graphicsView) |
| 110 | |
| 111 | self._init_trees() |
| 112 | |
| 113 | def _init_trees(self): |
| 114 | """初始化树形控件中的图形节点列表""" |
| 115 | # 1. 获取所有图标 |
| 116 | path = os.path.join(os.path.dirname(__file__), 'Data/icons') |
| 117 | icons = [os.path.join(path, name) for name in os.listdir(path)] |
| 118 | |
| 119 | # 2. 添加根节点 |
| 120 | for i in range(2): |
| 121 | item = QTreeWidgetItem(self.treeWidget) |
| 122 | item.setText(0, 'View %d' % i) |
| 123 | |
| 124 | # 3. 添加子节点作为容器用于存放图标 |
| 125 | itemc = QTreeWidgetItem(item) |
| 126 | child = ListWidget(self.treeWidget) |
| 127 | self.treeWidget.setItemWidget(itemc, 0, child) |
| 128 | |
| 129 | # 4. 添加图标 |
| 130 | for icon in icons: |
| 131 | item = QListWidgetItem(child) |
| 132 | item.setIcon(QIcon(icon)) |
| 133 | item.setToolTip(os.path.basename(icon)) |
| 134 | |
| 135 | self.treeWidget.expandAll() |
| 136 | |
| 137 | |
| 138 | if __name__ == '__main__': |