python---- pyqt 十字光标
# encoding: UTF-8
import sys,os import pyqtgraph as pg
import datetime as dt
import numpy as np
import traceback from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph.Point import Point
########################################################################
# 十字光标支持
########################################################################
class CrosshairTool(QtCore.QObject):
"""
此类给pg.PlotWidget()添加crossHair功能,PlotWidget实例需要初始化时传入
"""
signal = QtCore.pyqtSignal(type(tuple([])))
#----------------------------------------------------------------------
def __init__( self,pw,xAxis,viwe,parent=None):
self.__view = viwe
self.pw=pw
self.xData=xAxis
super(CrosshairTool, self).__init__()
self.xAxis = 0
self.yAxis = 0 # 在y轴动态mid跟随最新价显示最新价和最新时间
self.rects = [self.__view.vb.sceneBoundingRect()]
self.__textDate = pg.TextItem()
self.__textSig=pg.TextItem()
self.__textDate.setZValue(2)
self.__textSig.setZValue(2)
# 注册十字光标
self.vLine = pg.InfiniteLine(angle=90, movable=False)
self.hLine = pg.InfiniteLine(angle=0, movable=False)
self.vLine.setPos(0)
self.hLine.setPos(0)
self.__view.vb.addItem(self.vLine, ignoreBounds=True)
self.__view.vb.addItem(self.hLine, ignoreBounds=True)
self.__view.vb.addItem(self.__textDate, ignoreBounds=True)
self.__view.vb.addItem(self.__textSig, ignoreBounds=True)
self.proxy = pg.SignalProxy(self.pw.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)
# 跨线程刷新界面支持
self.signal.connect(self.update) # ----------------------------------------------------------------------
def update(self, pos):
"""刷新界面显示"""
xAxis, yAxis = pos
xAxis, yAxis = (self.xAxis, self.yAxis) if xAxis is None else (xAxis, yAxis)
self.moveTo(xAxis, yAxis) # ----------------------------------------------------------------------
def mouseMoved(self, evt): pos = evt[0] ## using signal proxy turns original arguments into a tuple
self.rects = [self.__view.vb.sceneBoundingRect()]
# if self.pw.sceneBoundingRect().contains(pos):
mousePoint = self.__view.vb.mapSceneToView(pos)
xAxis = mousePoint.x()
yAxis = mousePoint.y()
self.moveTo(xAxis, yAxis) # ---------------------------------------------------------------------- def moveTo(self, xAxis, yAxis):
xAxis, yAxis = (self.xAxis, self.yAxis) if xAxis is None else (xAxis, yAxis)
self.rects = [self.__view.sceneBoundingRect() ]
if not xAxis or not yAxis:
return
self.xAxis = xAxis
self.yAxis = yAxis
self.vhLinesSetXY(xAxis, yAxis)
self.plotVolue(xAxis,yAxis)
# ---------------------------------------------------------------------- def vhLinesSetXY(self, xAxis, yAxis):
"""水平和竖线位置设置"""
self.vLine.setPos(xAxis)
self.hLine.setPos(yAxis)
# ----------------------------------------------------------------------
def plotVolue(self,xAxis,yAxis):
if self.xData:
if int(round(xAxis)) <=len(self.xData)-1:
xValue =self.xData[int(round(xAxis))]
elif int(xAxis)>len(self.xData)-1:
xValue=self.xData[len(self.xData)-1]
else:
xValue = self.xData[int(xAxis)]
if (isinstance(xValue, dt.datetime)):
xValueText = dt.datetime.strftime(xValue, '%Y-%m-%d %H:%M:%S')
elif (isinstance(xValue, (str))):
xValueText = xValue
elif (isinstance(xValue, (int))):
xValueText = xValue
else:
xValueText = ""
else:
xValueText=xAxis
self.__textDate.setHtml(
'<div style="text-align: center">\
<span style="color: yellow; font-size: 20px;">x=%s</span>\
</div>' \
% (xValueText))
self.__textSig.setHtml(
'<div style="text-align: right">\
<span style="color: yellow; font-size: 20px;">y=%0.2f</span>\
</div>' \
% (yAxis))
# y,右上角显示
rightAxis = self.__view.getAxis('right')
rightAxisWidth = rightAxis.width()
rectTextsig = self.__textDate.boundingRect()
rectTextsigwidth = rectTextsig.width()
topRight = self.__view.vb.mapSceneToView(
QtCore.QPointF(self.rects[0].width() - (rightAxisWidth+rectTextsigwidth), self.rects[0].top()))
if yAxis<self.rects[0].top():
self.__textSig.anchor=Point((1,1));
else:
self.__textSig.anchor = Point((1, 0));
self.__textSig.setPos(topRight.x(), yAxis)
# X坐标
rectTextDate = self.__textDate.boundingRect()
rectTextDateHeight = rectTextDate.height()
bottomAxis = self.__view.getAxis('bottom')
bottomAxisHeight = bottomAxis.height()
bottomRight = self.__view.vb.mapSceneToView(QtCore.QPointF(self.rects[0].width(), \
self.rects[0].bottom() - (
bottomAxisHeight + rectTextDateHeight)))
# # 修改对称方式防止遮挡
if xAxis >self.rects[0].width():
self.__textDate.anchor = Point((1, 0))
else:
self.__textDate.anchor = Point((0, 0))
self.__textDate.setPos(xAxis, bottomRight.y())
python---- pyqt 十字光标的更多相关文章
- python pyqt绘制直方图
# -*- coding: utf-8 -*- """ In this example we draw two different kinds of histogram. ...
- 搭建python+PyQt+Eric平台
搭建python+PyQt+Eric平台 预备安装程序: 2.1.下载Python3.2 官方网站:http://www.python.org/ 下载地址:http://www.python.org/ ...
- VisionPro · C# · 图像显示十字光标
程序通过 CogRecordDisplay 显示控件显示视觉运行结果图像,当我们对调试时,可能需要用到图像中心十字对位光标. 本文通过VisionPro两个拟合线工具,一个拟合圆工具在图像中画出光标, ...
- python(pyqt)开发环境搭建
eric+pyqt 安装(python开发工具) 更多 0 Python python Eric是一个开源的.跨平台的python&ruby集成开发环境,基于python和pyqt运行.eri ...
- python pyqt
一.控件 1.单行文本框QLineText clear() 清除文本框中的内容contextMenuEvent() 右键菜单事件copy() 复制文本框中的内容cut() 剪切文本框中的内容paste ...
- Python & PyQt学习随笔:PyQt主程序的基本框架
在完成UI设计将UI通过PyUic转成Py文件后,由于这个生成的文件每次通过PyUic生成时都会被覆盖,因此应用的主程序必须另外单独编写py文件.需要将UI生成的文件import到主程序的py文件中. ...
- Python+PyQt 数据库基本操作
Sqlite: 使用Python的sqlite3: 需要注意下commit方式与qt稍有不同 import sqlite3 class DBManager(): def __init__(self): ...
- 调整altium designer15的十字光标大小
在左上角的DXP下preferences中调整.首先打开该窗口. 1.原理图:schematic-----graphical editing,此窗口中cursor栏有个cursor type,其下拉菜 ...
- Python——PYQT:控件基本使用
QtGui.QComboBox控件常用函数: .addItem(string) #添加字符串项到Item .addItems(list) #添加列表或元组元素到Item .clear() #清除所有I ...
随机推荐
- cocos2d-x C++ 获取网络图片缓存并展示
#ifndef __HttpGetImg__ #define __HttpGetImg__ #include "cocos2d.h" #include "HttpRequ ...
- 用原生js+canvas实现五子棋
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 解决:启用多线程调用webBrowsers函数报错:指定的转换无效
这里就需要委托. 定义一个 委托.加载之后给他绑定一个方法Callback,也就是所说的回掉函数. 然后写一个线程,线程需要一个object 的参数.将你定义的委托当作参数传进线程中的方法. 在线程中 ...
- Jar包的手动导入
(1)打开项目的file 找到project structure (2)进行以下操作
- mongodb和python交互
一.安装pymongo包 sudo pip install pymongo 二.新增数据: 增加一条: from pymongo import MongoClient client = MongoCl ...
- 18.12.09-C语言练习:黑洞数 / Kaprekar问题
题目: 程序: #include <stdio.h> int main(void) { int n, a, b, c, t, A, B; printf("输入一个三位数整数:&q ...
- OpenStack-Neutron-VPNaaS-代码
目前juno只支持ipsec的vpn 但是其实稍微修改代码pptp/openvpn/gre也都是可以支持的,下面看看vpn服务的代码流程: 默认我们创建好了ide策略.ipsec策略和vpn服务,因 ...
- t-SNE完整笔记
http://www.datakit.cn/blog/2017/02/05/t_sne_full.html t-SNE(t-distributed stochastic neighbor embedd ...
- mui返回上个页面并刷新数据
转 https://blog.csdn.net/mercedescc/article/details/82769264 今天写项目遇到个问题,就是B页面支付操作完成以后,点击返回按钮要到A页面,此时A ...
- 韩顺平Linux学习笔记
第 一 章 Linux开山篇 1.1 Linux课程的内容介绍 1.2Linux的学习方向 1.2.1. Linux运维工程师:主要做大公司中的电脑系统维护,保证服务器的正常运行,如服务器的优化 ...