# 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. html-webpack-plugin插件使用

    项目使用hightopo框架,使用webpack打包.这里的场景是:点击预览按钮,页面会打开一个新页面. 但是由于使用了webpack打包,所以直接使用以下代码是不行的.报404 window.ope ...

  2. phpstorm----------phpstorm设置自动更新的ssh信息如何修改--后续增加如何设置自动更新

    1.如何设置phpstorm将本地代码时时同步到远程服务器 注意下面一定要打勾 点击下一步,然后还有一个页面,然后不用做任何操作,直接点击完成.中途有个页面是输入远程服务器ip账号密码链接方式的,那个 ...

  3. Unity 利用UGUI打包图集,动态加载sprite资源

    今天做了一个UI界面,这个界面是好友界面,该界面上有若干个好友item. 需要对每个tem的头像对象(image)动态显示对应的头像.尝试利用UGUI的图集来加载,具体实现如下: 1.首先,需要知道S ...

  4. 解决SVN提交和更新代码冲突?

    解决冲突有三种选择: 1.放弃自己的更新,使用svn revert(回滚),然后提交.在这种方式下不需要使用svn resolved(解决) 2.放弃自己的更新,使用别人的更新.使用最新获取的版本覆盖 ...

  5. vim基本命令总结

    编辑模式下i 从光标所在位置前开始插入文本I 将光标移动到当前行行首,然后在其前插入文本a 用于在光标当前所在位置之后追加新文本A 将光标移动到所在行行尾,在那里插入新文本o 在光标所在行的下面新开一 ...

  6. IIS的地址指向

    地址指向 1)AuthwebAPI  修改web.xml文件 <connectionStrings> data source 改成当前虚拟环境的IP指向 </connectionSt ...

  7. APScheduler

    目录 APScheduler简介 支持的后端存储作业 集成的Python框架 APScheduler下载安装 APScheduler组件 各组件简介 调度器 作业存储器 执行器 触发器 使用 添加作业 ...

  8. C#线程同步(3)- 互斥量 Mutex

    文章原始出处 http://xxinside.blogbus.com/logs/47162540.html 预备知识:C#线程同步(1)- 临界区&Lock,C#线程同步(2)- 临界区&am ...

  9. String.StartsWith 方法

    startsWith() 方法用于检测字符串是否以指定的前缀开始. 语法 public boolean startsWith(String prefix, int toffset) 或 public ...

  10. return -1 、return 1 、 return 0 的区别

    根据所定义函数的需求返回不同的值. 一般0和-1或者0和1使用 0和-1使用时: 0一般表示成功执行 -1一般表示不成功 0和1使用时: 1真 0假