json生成QTreeWidget #10

This commit is contained in:
Irony 2018-04-09 00:47:30 +08:00
parent 482a95a5f0
commit a2cf69f30a
6 changed files with 218 additions and 0 deletions

View file

@ -14,6 +14,7 @@ encoding//QGraphicsView\u7EC3\u4E60/\u6DFB\u52A0QWidget.py=utf-8
encoding//QGraphicsView\u7EC3\u4E60/\u7B80\u5355\u56FE\u50CF\u5904\u7406/SimpleImagePs.py=utf-8
encoding//QGraphicsView\u7EC3\u4E60/\u7B80\u5355\u56FE\u50CF\u5904\u7406/SimpleImageThread.py=utf-8
encoding//QGraphicsView\u7EC3\u4E60/\u7B80\u5355\u56FE\u50CF\u5904\u7406/SimpleImageView.py=utf-8
encoding//json\u751F\u6210QTreeWidget/json\u751F\u6210\u6811\u5F62\u7ED3\u6784.py=utf-8
encoding//tmp/LocalClient.py=utf-8
encoding//tmp/Material/Effect/LineEffect.py=utf-8
encoding//tmp/Material/Utils/Colors.py=utf-8
@ -22,6 +23,7 @@ encoding//tmp/Material/Widget/LineEdit.py=utf-8
encoding//tmp/Material/test/TestLineEdit.py=utf-8
encoding//tmp/QtServer.py=utf-8
encoding//tmp/error.py=utf-8
encoding//tmp/json\u751F\u6210\u6811\u5F62\u7ED3\u6784.py=utf-8
encoding//tmp/\u622A\u56FE\u753B\u77E9\u5F62/DrawRectangle.py=utf-8
encoding//tmp/\u7B80\u5355\u63D2\u4EF6/Widget.py=utf-8
encoding//tmp/\u8BFB\u53D6\u526A\u5207\u677F\u56FE\u7247.py=utf-8

View file

@ -0,0 +1,8 @@
# 通过json数据生成QTreeWidget树形结构
### [Python3.4.4 or Python3.5][PyQt5]
emmmmmm,原理就是那样,,解析每一层json数据中的list,美化就不想弄了.
# 截图
![截图1](ScreenShot/1.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

View file

@ -0,0 +1,56 @@
[
{
"name": "仪表盘",
"icon": "xxx.png"
},
{
"name": "微信管理",
"icon": "weixin.png",
"badge": [
"火爆",
"#F8AC54"
],
"items": [
{
"name": "爬虫项目管理",
"icon": "xxx.png",
"badge": [
"推荐",
"#23C6C9"
],
"items": [
{
"name": "腾讯课堂"
},
{
"name": "淘宝"
},
{
"name": "京东"
},
{
"name": "天猫"
},
{
"name": "拉钩"
},
{
"name": "BOSS直聘"
}
]
}
]
},
{
"name": "爬虫部署管理",
"icon": "xxx.png"
},
{
"name": "爬虫监控管理",
"icon": "xxx.png"
},
{
"name": "PyQt",
"url": "https://github.com/892768447/PyQt"
}
]

View file

@ -0,0 +1,152 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年4月8日
@author: Irony
@site: https://github.com/892768447
@email: 892768447@qq.com
@file:
@description:
"""
import json
import webbrowser
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QWidget,\
QLabel, QSpacerItem, QSizePolicy, QHBoxLayout
import chardet
__Author__ = """By: Irony
QQ: 892768447
Email: 892768447@qq.com"""
__Copyright__ = "Copyright (c) 2018 Irony"
__Version__ = "Version 1.0"
class ItemWidget(QWidget):
"""自定义的item"""
def __init__(self, text, badge, *args, **kwargs):
super(ItemWidget, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(QLabel(text, self, styleSheet='color: white;'))
layout.addSpacerItem(QSpacerItem(
60, 1, QSizePolicy.Maximum, QSizePolicy.Minimum))
if badge and len(badge) == 2: # 后面带颜色的标签
layout.addWidget(QLabel(
badge[0], self, alignment=Qt.AlignCenter,
styleSheet="""min-width: 80px;
max-width: 80px;
min-height: 38px;
max-height: 38px;
color: white;
border:none;
border-radius: 4px;
background: %s""" % badge[1]
))
class JsonTreeWidget(QTreeWidget):
def __init__(self, *args, **kwargs):
super(JsonTreeWidget, self).__init__(*args, **kwargs)
self.setEditTriggers(self.NoEditTriggers)
self.header().setVisible(False)
# 帮点单击事件
self.itemClicked.connect(self.onItemClicked)
def onItemClicked(self, item):
"""item单击事件"""
if item.url: # 调用浏览器打开网址
webbrowser.open_new_tab(item.url)
def parseData(self, datas, parent=None):
"""解析json数据"""
for data in datas:
url = data.get('url', '')
items = data.get('items', [])
# 生成item
_item = QTreeWidgetItem(parent)
_item.setIcon(0, QIcon(data.get('icon', '')))
_widget = ItemWidget(
data.get('name', ''),
data.get('badge', []),
self
)
_item.url = url # 可以直接设置变量值
self.setItemWidget(_item, 0, _widget)
if url:
continue # 跳过
# 解析儿子
if items:
self.parseData(items, _item)
def loadData(self, path):
"""加载json数据"""
datas = open(path, 'rb').read()
datas = datas.decode(chardet.detect(datas).get('encoding', 'utf-8'))
self.parseData(json.loads(datas), self)
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
app.setStyleSheet("""QTreeView {
outline: 0px;
background: rgb(47, 64, 78);
}
QTreeView::item {
min-height: 92px;
}
QTreeView::item:hover {
background: rgb(41, 56, 71);
}
QTreeView::item:selected {
background: rgb(41, 56, 71);
}
QTreeView::item:selected:active{
background: rgb(41, 56, 71);
}
QTreeView::item:selected:!active{
background: rgb(41, 56, 71);
}
QTreeView::branch:open:has-children {
background: rgb(41, 56, 71);
}
QTreeView::branch:has-siblings:!adjoins-item {
background: green;
}
QTreeView::branch:closed:has-children:has-siblings {
background: rgb(47, 64, 78);
}
QTreeView::branch:has-children:!has-siblings:closed {
background: rgb(47, 64, 78);
}
QTreeView::branch:open:has-children:has-siblings {
background: rgb(41, 56, 71);
}
QTreeView::branch:open:has-children:!has-siblings {
background: rgb(41, 56, 71);
}
QTreeView:branch:hover {
background: rgb(41, 56, 71);
}
QTreeView:branch:selected {
background: rgb(41, 56, 71);
}
""")
w = JsonTreeWidget()
w.show()
w.loadData('data.json')
sys.exit(app.exec_())

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB