# 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 十字光标的更多相关文章

  1. python pyqt绘制直方图

    # -*- coding: utf-8 -*- """ In this example we draw two different kinds of histogram. ...

  2. 搭建python+PyQt+Eric平台

    搭建python+PyQt+Eric平台 预备安装程序: 2.1.下载Python3.2 官方网站:http://www.python.org/ 下载地址:http://www.python.org/ ...

  3. VisionPro · C# · 图像显示十字光标

    程序通过 CogRecordDisplay 显示控件显示视觉运行结果图像,当我们对调试时,可能需要用到图像中心十字对位光标. 本文通过VisionPro两个拟合线工具,一个拟合圆工具在图像中画出光标, ...

  4. python(pyqt)开发环境搭建

    eric+pyqt 安装(python开发工具) 更多 0 Python python Eric是一个开源的.跨平台的python&ruby集成开发环境,基于python和pyqt运行.eri ...

  5. python pyqt

    一.控件 1.单行文本框QLineText clear() 清除文本框中的内容contextMenuEvent() 右键菜单事件copy() 复制文本框中的内容cut() 剪切文本框中的内容paste ...

  6. Python & PyQt学习随笔:PyQt主程序的基本框架

    在完成UI设计将UI通过PyUic转成Py文件后,由于这个生成的文件每次通过PyUic生成时都会被覆盖,因此应用的主程序必须另外单独编写py文件.需要将UI生成的文件import到主程序的py文件中. ...

  7. Python+PyQt 数据库基本操作

    Sqlite: 使用Python的sqlite3: 需要注意下commit方式与qt稍有不同 import sqlite3 class DBManager(): def __init__(self): ...

  8. 调整altium designer15的十字光标大小

    在左上角的DXP下preferences中调整.首先打开该窗口. 1.原理图:schematic-----graphical editing,此窗口中cursor栏有个cursor type,其下拉菜 ...

  9. Python——PYQT:控件基本使用

    QtGui.QComboBox控件常用函数: .addItem(string) #添加字符串项到Item .addItems(list) #添加列表或元组元素到Item .clear() #清除所有I ...

随机推荐

  1. DHCP和TFTP服务

    DHCP服务 主要用途:用于内部网络和网络服务供应商自动分配IP地址给用户 用于内部网络管理员作为对所有电脑作集中管理的手段 使用场景:自动化安装系统 解决IPV4资源不足问题 DHCP共有八种报文: ...

  2. 使用MyEclipse新建maven项目时报An internal error occurred during: "Retrieving archetypes:". GC overhead limit

    前几天在上手maven时,遇到了一个十分头疼的问题,我的myeclipse配置的是自己安装的插件 ,总是报 " An internal error occurred during: &quo ...

  3. hibernate框架的简单入门

    1.什么是框架 框架是一个半成品,框架帮我们实现了一部分的功能. 2.使用框架的最大好处 使用框架的最大好处就是,少写一部分代码但仍能实现我们所需要实现的功能. 3.什么是hiberbnate框架 ( ...

  4. LR12脚本录制流程

    准备工作: 工具:win7系统.loadrunner 12.Firefox浏览器.飞机订票系统. 打开飞机订票系统 开始录制脚本 步骤一:打开loadrunner(Virtual User Gener ...

  5. Azure上搭建ActiveMQ集群-基于ZooKeeper配置ActiveMQ高可用性集群

    ActiveMQ从5.9.0版本开始,集群实现方式取消了传统的Master-Slave方式,增加了基于ZooKeeper+LevelDB的实现方式. 本文主要介绍了在Windows环境下配置基于Zoo ...

  6. idea用到的快捷键

    之前一直用的eclipse,早就听说idea更智能,更便捷,于是,下载了idea,然后再破解,现在就慢慢抛弃eclipse,平时就用idea进行编码. idea的快捷键与eclipse还是有较大不同, ...

  7. (Review cs231n) Object Detection

    目标:我们有几个类别,然后我们要在这张图中找到这些类的所有实例 解决思路:是否可以按照回归的思路进行求解呢? 但是受限制于确定的种类输出问题. 方法:分类和回归是解决问题的两个套路,我们现在对于目标的 ...

  8. css中height 100vh的应用场景,动态高度百分比布局,浏览器视区大小单位

    css中height 100vh的应用场景,动态高度百分比布局,浏览器视区大小单位 height:100vh 一些只能vw, vh才能完成的应用场景: 1. 场景之:元素的尺寸限制 vw vh 主要是 ...

  9. Global.asax

    ASP.NET Global.asax 文件使用方法 - .net 标签:               asp.net.netapplicationauthenticationsessionobjec ...

  10. leetcode 1——两数之和

    问题描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...