Skip to content

Commit fb6a09b

Browse files
committed
add treeWidget Demo
1 parent 08fb61f commit fb6a09b

File tree

6 files changed

+250
-2
lines changed

6 files changed

+250
-2
lines changed

PyQtGraph/README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@
1515

1616
## 目录
1717
- [鼠标获取X轴坐标](#1、鼠标获取X轴坐标)
18+
- [禁止右键点击功能、鼠标滚轮,添加滚动条等功能](#2、禁止右键点击功能、鼠标滚轮,添加滚动条等功能)
1819

1920
## 1、鼠标获取X轴坐标
2021
[运行 mouseFlow.py](mouseFlow.py)
2122

22-
![mouseFlow](ScreenShot/mouseFlow.gif)
23+
![mouseFlow](ScreenShot/mouseFlow.gif)
24+
25+
## 2、禁止右键点击功能、鼠标滚轮,添加滚动条等功能
26+
[运行 graph1.py](graph1.py)
27+
28+
![mouseFlow](ScreenShot/function.gif)
29+
30+
## 2、不用修改源码,重加载,解决右键保存图片异常;解决自定义坐标轴密集显示;禁止鼠标事件;
31+
[加载 tools.py](tools.py)

PyQtGraph/ScreenShot/function.gif

330 KB
Loading

PyQtGraph/graph1.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
'''
4+
Created on 2019年5月21日
5+
@author: weike32
6+
@site: https://pyqt5.com ,https://github.com/weike32
7+
@email: 394967319@qq.com
8+
@file: CopyContent
9+
@description: 禁止右键,添加滑动窗口,点击按钮生成图片,自定义Y轴坐标,背景颜色调整
10+
'''
11+
import sys
12+
from PyQt5.QtWidgets import QDialog, QApplication, QWidget
13+
from qtpy import QtWidgets
14+
import pyqtgraph as pg
15+
from PyQtGraph.graphAnalysis import graph_Form
16+
17+
class CustomViewBox(pg.ViewBox):
18+
def __init__(self, *args, **kwds):
19+
pg.ViewBox.__init__(self, *args, **kwds)
20+
self.RectMode = 3
21+
self.setMouseMode(self.RectMode)
22+
23+
def mouseClickEvent(self, ev):
24+
if ev.button() == pg.QtCore.Qt.RightButton:
25+
self.autoRange()
26+
27+
def mouseDragEvent(self, ev):
28+
pg.ViewBox.mouseDragEvent(self, ev)
29+
30+
def wheelEvent(self,ev, axis=None):
31+
# pg.ViewBox.wheelEvent(self, ev, axis)
32+
ev.ignore()
33+
34+
class graphAnalysis(QDialog,graph_Form):
35+
def __init__(self):
36+
super(graphAnalysis, self).__init__()
37+
self.setupUi(self)
38+
self.pushButton_7.clicked.connect(self.test)
39+
self.tabWidget.clear()
40+
41+
def test(self):
42+
tab1 = QtWidgets.QWidget()
43+
scrollArea = QtWidgets.QScrollArea(tab1)
44+
scrollArea.setMinimumSize(984,550)
45+
scrollArea.setWidgetResizable(True)
46+
labelsContainer = QWidget()
47+
labelsContainer.setMinimumSize(0,1500)
48+
scrollArea.setWidget(labelsContainer)
49+
layout = QtWidgets.QVBoxLayout(labelsContainer)
50+
time = ['2019-04-20 08:09:00', '2019-04-20 08:09:00', '2019-04-20 08:09:00', '2019-04-20 08:09:00']
51+
value = [1.2, 2, 1, 4]
52+
xdict = dict(enumerate(time))
53+
ticks = [list(zip(range(4), tuple(time)))]
54+
vb = CustomViewBox()
55+
plt = pg.PlotWidget(title="标题这里填写",viewBox=vb)
56+
plt.setBackground(background=None)
57+
plt.plot(list(xdict.keys()), value)
58+
plt.getPlotItem().getAxis("bottom").setTicks(ticks)
59+
temp = QtWidgets.QWidget()
60+
temp.setMinimumSize(900,300)
61+
temp.setMaximumSize(900,300)
62+
layout1 = QtWidgets.QVBoxLayout(temp)
63+
layout1.addWidget(plt)
64+
layout.addWidget(temp)
65+
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
66+
layout.addItem(spacerItem)
67+
self.tabWidget.addTab(tab1, '这里tabWidget修改标签')
68+
69+
if __name__ =="__main__":
70+
app = QApplication(sys.argv)
71+
w = graphAnalysis()
72+
w.show()
73+
sys.exit(app.exec_())

PyQtGraph/graphAnalysis.ui

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>Form</class>
4+
<widget class="QWidget" name="Form">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>1024</width>
10+
<height>768</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Form</string>
15+
</property>
16+
<layout class="QVBoxLayout" name="verticalLayout">
17+
<property name="spacing">
18+
<number>20</number>
19+
</property>
20+
<property name="leftMargin">
21+
<number>20</number>
22+
</property>
23+
<property name="topMargin">
24+
<number>20</number>
25+
</property>
26+
<property name="rightMargin">
27+
<number>20</number>
28+
</property>
29+
<property name="bottomMargin">
30+
<number>20</number>
31+
</property>
32+
<item>
33+
<widget class="QLabel" name="label">
34+
<property name="font">
35+
<font>
36+
<pointsize>15</pointsize>
37+
</font>
38+
</property>
39+
<property name="text">
40+
<string>图形分析</string>
41+
</property>
42+
<property name="alignment">
43+
<set>Qt::AlignCenter</set>
44+
</property>
45+
</widget>
46+
</item>
47+
<item>
48+
<widget class="QTabWidget" name="tabWidget">
49+
<property name="currentIndex">
50+
<number>0</number>
51+
</property>
52+
<widget class="QWidget" name="tab">
53+
<attribute name="title">
54+
<string>折线图</string>
55+
</attribute>
56+
</widget>
57+
<widget class="QWidget" name="tab_2">
58+
<attribute name="title">
59+
<string>Tab 2</string>
60+
</attribute>
61+
</widget>
62+
</widget>
63+
</item>
64+
<item>
65+
<widget class="QPushButton" name="pushButton_7">
66+
<property name="minimumSize">
67+
<size>
68+
<width>0</width>
69+
<height>80</height>
70+
</size>
71+
</property>
72+
<property name="font">
73+
<font>
74+
<pointsize>15</pointsize>
75+
</font>
76+
</property>
77+
<property name="text">
78+
<string>分析</string>
79+
</property>
80+
</widget>
81+
</item>
82+
</layout>
83+
</widget>
84+
<resources/>
85+
<connections/>
86+
</ui>

PyQtGraph/mouseFlow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# encoding: utf-8
33
'''
4-
Created on 2017年5月2日
4+
Created on 2019年5月2日
55
@author: weike32
66
@site: https://pyqt5.com ,https://github.com/weike32
77
@email: 394967319@qq.com

PyQtGraph/tools.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
'''
4+
Created on 2019年5月21日
5+
@author: weike32
6+
@site: https://pyqt5.com ,https://github.com/weike32
7+
@email: 394967319@qq.com
8+
@file: CopyContent
9+
@description: 工具类
10+
'''
11+
from pyqtgraph.exporters.ImageExporter import ImageExporter, Exporter
12+
from pyqtgraph.parametertree import Parameter
13+
import pyqtgraph as pg
14+
#不用修改源码,重加载,解决右键保存图片异常
15+
def widthChanged(self):
16+
sr = self.getSourceRect()
17+
ar = float(sr.height()) / sr.width()
18+
self.params.param('height').setValue(int(self.params['width'] * ar), blockSignal=self.heightChanged)
19+
20+
def heightChanged(self):
21+
sr = self.getSourceRect()
22+
ar = float(sr.width()) / sr.height()
23+
self.params.param('width').setValue(int(self.params['height'] * ar), blockSignal=self.widthChanged)
24+
25+
def New__init__(self, item):
26+
Exporter.__init__(self, item)
27+
tr = self.getTargetRect()
28+
if isinstance(item, pg.Qt.QtGui.QGraphicsItem):
29+
scene = item.scene()
30+
else:
31+
scene = item
32+
bgbrush = scene.views()[0].backgroundBrush()
33+
bg = bgbrush.color()
34+
if bgbrush.style() == pg.Qt.QtCore.Qt.NoBrush:
35+
bg.setAlpha(0)
36+
37+
self.params = Parameter(name='params', type='group', children=[
38+
{'name': 'width', 'type': 'int', 'value': int(tr.width()), 'limits': (0, None)},
39+
{'name': 'height', 'type': 'int', 'value': int(tr.height()), 'limits': (0, None)},
40+
{'name': 'antialias', 'type': 'bool', 'value': True},
41+
{'name': 'background', 'type': 'color', 'value': bg},
42+
])
43+
self.params.param('width').sigValueChanged.connect(self.widthChanged)
44+
self.params.param('height').sigValueChanged.connect(self.heightChanged)
45+
ImageExporter.heightChanged = heightChanged
46+
ImageExporter.widthChanged = widthChanged
47+
ImageExporter.__init__ = New__init__
48+
49+
#解决自定义坐标轴密集显示
50+
class MyStringAxis(pg.AxisItem):
51+
def __init__(self, xdict, *args, **kwargs):
52+
pg.AxisItem.__init__(self, *args, **kwargs)
53+
self.xdict = xdict
54+
55+
def tickStrings(self, values, scale, spacing):
56+
strings = []
57+
for v in values:
58+
vs = v * scale
59+
if vs in self.xdict.keys():
60+
vstr = self.xdict[vs]
61+
else:
62+
vstr = ""
63+
strings.append(vstr)
64+
return strings
65+
# 禁止鼠标事件
66+
class CustomViewBox(pg.ViewBox):
67+
def __init__(self, *args, **kwds):
68+
pg.ViewBox.__init__(self, *args, **kwds)
69+
self.RectMode = 3
70+
self.setMouseMode(self.RectMode)
71+
72+
def mouseClickEvent(self, ev):
73+
if ev.button() == pg.QtCore.Qt.RightButton:
74+
self.autoRange()
75+
76+
def mouseDragEvent(self, ev):
77+
pg.ViewBox.mouseDragEvent(self, ev)
78+
79+
def wheelEvent(self,ev, axis=None):
80+
pg.ViewBox.wheelEvent(self, ev, axis)

0 commit comments

Comments
 (0)