add treeWidget Demo
This commit is contained in:
parent
08fb61f870
commit
fb6a09be04
6 changed files with 250 additions and 2 deletions
|
@ -15,8 +15,17 @@
|
|||
|
||||
## 目录
|
||||
- [鼠标获取X轴坐标](#1、鼠标获取X轴坐标)
|
||||
- [禁止右键点击功能、鼠标滚轮,添加滚动条等功能](#2、禁止右键点击功能、鼠标滚轮,添加滚动条等功能)
|
||||
|
||||
## 1、鼠标获取X轴坐标
|
||||
[运行 mouseFlow.py](mouseFlow.py)
|
||||
|
||||
![mouseFlow](ScreenShot/mouseFlow.gif)
|
||||
|
||||
## 2、禁止右键点击功能、鼠标滚轮,添加滚动条等功能
|
||||
[运行 graph1.py](graph1.py)
|
||||
|
||||
![mouseFlow](ScreenShot/function.gif)
|
||||
|
||||
## 2、不用修改源码,重加载,解决右键保存图片异常;解决自定义坐标轴密集显示;禁止鼠标事件;
|
||||
[加载 tools.py](tools.py)
|
BIN
PyQtGraph/ScreenShot/function.gif
Normal file
BIN
PyQtGraph/ScreenShot/function.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 330 KiB |
73
PyQtGraph/graph1.py
Normal file
73
PyQtGraph/graph1.py
Normal file
|
@ -0,0 +1,73 @@
|
|||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
'''
|
||||
Created on 2019年5月21日
|
||||
@author: weike32
|
||||
@site: https://pyqt5.com ,https://github.com/weike32
|
||||
@email: 394967319@qq.com
|
||||
@file: CopyContent
|
||||
@description: 禁止右键,添加滑动窗口,点击按钮生成图片,自定义Y轴坐标,背景颜色调整
|
||||
'''
|
||||
import sys
|
||||
from PyQt5.QtWidgets import QDialog, QApplication, QWidget
|
||||
from qtpy import QtWidgets
|
||||
import pyqtgraph as pg
|
||||
from PyQtGraph.graphAnalysis import graph_Form
|
||||
|
||||
class CustomViewBox(pg.ViewBox):
|
||||
def __init__(self, *args, **kwds):
|
||||
pg.ViewBox.__init__(self, *args, **kwds)
|
||||
self.RectMode = 3
|
||||
self.setMouseMode(self.RectMode)
|
||||
|
||||
def mouseClickEvent(self, ev):
|
||||
if ev.button() == pg.QtCore.Qt.RightButton:
|
||||
self.autoRange()
|
||||
|
||||
def mouseDragEvent(self, ev):
|
||||
pg.ViewBox.mouseDragEvent(self, ev)
|
||||
|
||||
def wheelEvent(self,ev, axis=None):
|
||||
# pg.ViewBox.wheelEvent(self, ev, axis)
|
||||
ev.ignore()
|
||||
|
||||
class graphAnalysis(QDialog,graph_Form):
|
||||
def __init__(self):
|
||||
super(graphAnalysis, self).__init__()
|
||||
self.setupUi(self)
|
||||
self.pushButton_7.clicked.connect(self.test)
|
||||
self.tabWidget.clear()
|
||||
|
||||
def test(self):
|
||||
tab1 = QtWidgets.QWidget()
|
||||
scrollArea = QtWidgets.QScrollArea(tab1)
|
||||
scrollArea.setMinimumSize(984,550)
|
||||
scrollArea.setWidgetResizable(True)
|
||||
labelsContainer = QWidget()
|
||||
labelsContainer.setMinimumSize(0,1500)
|
||||
scrollArea.setWidget(labelsContainer)
|
||||
layout = QtWidgets.QVBoxLayout(labelsContainer)
|
||||
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']
|
||||
value = [1.2, 2, 1, 4]
|
||||
xdict = dict(enumerate(time))
|
||||
ticks = [list(zip(range(4), tuple(time)))]
|
||||
vb = CustomViewBox()
|
||||
plt = pg.PlotWidget(title="标题这里填写",viewBox=vb)
|
||||
plt.setBackground(background=None)
|
||||
plt.plot(list(xdict.keys()), value)
|
||||
plt.getPlotItem().getAxis("bottom").setTicks(ticks)
|
||||
temp = QtWidgets.QWidget()
|
||||
temp.setMinimumSize(900,300)
|
||||
temp.setMaximumSize(900,300)
|
||||
layout1 = QtWidgets.QVBoxLayout(temp)
|
||||
layout1.addWidget(plt)
|
||||
layout.addWidget(temp)
|
||||
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
|
||||
layout.addItem(spacerItem)
|
||||
self.tabWidget.addTab(tab1, '这里tabWidget修改标签')
|
||||
|
||||
if __name__ =="__main__":
|
||||
app = QApplication(sys.argv)
|
||||
w = graphAnalysis()
|
||||
w.show()
|
||||
sys.exit(app.exec_())
|
86
PyQtGraph/graphAnalysis.ui
Normal file
86
PyQtGraph/graphAnalysis.ui
Normal file
|
@ -0,0 +1,86 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>Form</class>
|
||||
<widget class="QWidget" name="Form">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1024</width>
|
||||
<height>768</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<property name="spacing">
|
||||
<number>20</number>
|
||||
</property>
|
||||
<property name="leftMargin">
|
||||
<number>20</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>20</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>20</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>20</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>15</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>图形分析</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="tab">
|
||||
<attribute name="title">
|
||||
<string>折线图</string>
|
||||
</attribute>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab_2">
|
||||
<attribute name="title">
|
||||
<string>Tab 2</string>
|
||||
</attribute>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_7">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>80</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>15</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>分析</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
'''
|
||||
Created on 2017年5月2日
|
||||
Created on 2019年5月2日
|
||||
@author: weike32
|
||||
@site: https://pyqt5.com ,https://github.com/weike32
|
||||
@email: 394967319@qq.com
|
||||
|
|
80
PyQtGraph/tools.py
Normal file
80
PyQtGraph/tools.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
'''
|
||||
Created on 2019年5月21日
|
||||
@author: weike32
|
||||
@site: https://pyqt5.com ,https://github.com/weike32
|
||||
@email: 394967319@qq.com
|
||||
@file: CopyContent
|
||||
@description: 工具类
|
||||
'''
|
||||
from pyqtgraph.exporters.ImageExporter import ImageExporter, Exporter
|
||||
from pyqtgraph.parametertree import Parameter
|
||||
import pyqtgraph as pg
|
||||
#不用修改源码,重加载,解决右键保存图片异常
|
||||
def widthChanged(self):
|
||||
sr = self.getSourceRect()
|
||||
ar = float(sr.height()) / sr.width()
|
||||
self.params.param('height').setValue(int(self.params['width'] * ar), blockSignal=self.heightChanged)
|
||||
|
||||
def heightChanged(self):
|
||||
sr = self.getSourceRect()
|
||||
ar = float(sr.width()) / sr.height()
|
||||
self.params.param('width').setValue(int(self.params['height'] * ar), blockSignal=self.widthChanged)
|
||||
|
||||
def New__init__(self, item):
|
||||
Exporter.__init__(self, item)
|
||||
tr = self.getTargetRect()
|
||||
if isinstance(item, pg.Qt.QtGui.QGraphicsItem):
|
||||
scene = item.scene()
|
||||
else:
|
||||
scene = item
|
||||
bgbrush = scene.views()[0].backgroundBrush()
|
||||
bg = bgbrush.color()
|
||||
if bgbrush.style() == pg.Qt.QtCore.Qt.NoBrush:
|
||||
bg.setAlpha(0)
|
||||
|
||||
self.params = Parameter(name='params', type='group', children=[
|
||||
{'name': 'width', 'type': 'int', 'value': int(tr.width()), 'limits': (0, None)},
|
||||
{'name': 'height', 'type': 'int', 'value': int(tr.height()), 'limits': (0, None)},
|
||||
{'name': 'antialias', 'type': 'bool', 'value': True},
|
||||
{'name': 'background', 'type': 'color', 'value': bg},
|
||||
])
|
||||
self.params.param('width').sigValueChanged.connect(self.widthChanged)
|
||||
self.params.param('height').sigValueChanged.connect(self.heightChanged)
|
||||
ImageExporter.heightChanged = heightChanged
|
||||
ImageExporter.widthChanged = widthChanged
|
||||
ImageExporter.__init__ = New__init__
|
||||
|
||||
#解决自定义坐标轴密集显示
|
||||
class MyStringAxis(pg.AxisItem):
|
||||
def __init__(self, xdict, *args, **kwargs):
|
||||
pg.AxisItem.__init__(self, *args, **kwargs)
|
||||
self.xdict = xdict
|
||||
|
||||
def tickStrings(self, values, scale, spacing):
|
||||
strings = []
|
||||
for v in values:
|
||||
vs = v * scale
|
||||
if vs in self.xdict.keys():
|
||||
vstr = self.xdict[vs]
|
||||
else:
|
||||
vstr = ""
|
||||
strings.append(vstr)
|
||||
return strings
|
||||
# 禁止鼠标事件
|
||||
class CustomViewBox(pg.ViewBox):
|
||||
def __init__(self, *args, **kwds):
|
||||
pg.ViewBox.__init__(self, *args, **kwds)
|
||||
self.RectMode = 3
|
||||
self.setMouseMode(self.RectMode)
|
||||
|
||||
def mouseClickEvent(self, ev):
|
||||
if ev.button() == pg.QtCore.Qt.RightButton:
|
||||
self.autoRange()
|
||||
|
||||
def mouseDragEvent(self, ev):
|
||||
pg.ViewBox.mouseDragEvent(self, ev)
|
||||
|
||||
def wheelEvent(self,ev, axis=None):
|
||||
pg.ViewBox.wheelEvent(self, ev, axis)
|
Loading…
Reference in a new issue