# 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. jmeter压测mysql报can not be represented as java.sql.Timestame错误解决方法

    JDBC Request  测试mysql时报以下问题? jmeter报错信息: 解决方法: 在数据库url后拼接上字符串?characterEncoding=utf8&zeroDateTim ...

  2. MUI 返回顶部

    //绑定滚动到顶部按钮事件 if ($("#scroll-up").length > 0) { var scrollToTopBox = $("#scroll-up ...

  3. git----------如何安装gitlab,使用步骤。

    1.配置yum源        vim /etc/yum.repos.d/gitlab-ce.repo 2.复制以下内容到打开的文件中: [gitlab-ce]     name=Gitlab CE ...

  4. [CentOS] rsync同步目录进行备份文件

    操作不难,网上一堆.这里列几个 CentOS7 参考地址: https://www.server-world.info/en/note?os=CentOS_7&p=rsync Copy fil ...

  5. shell脚本中各类括号的作用(小结)

    技巧小结: 字符串比较用双中括号[[ ]]:算数比较用单中括号[ ]——左右留空格 算数运算用双小括号(( )) :shell命令及输出用小括号( )——左右不留空格 快速替换用花括号{ }——左右留 ...

  6. html5 旋转导航练习

    ul{    list-style: none;    font-size: 24px;    font-weight: bold; }a{    text-decoration: none;}li{ ...

  7. 2018-2019-2 网络对抗技术 20165305 Exp6 信息搜集与漏洞扫描

    1.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具 ...

  8. Linux SPI初始化及接口函数代码细究

    2012-01-08 22:11:38 目的:我需要掌握spi驱动相关数据结构关系,及在哪部分函数中把这些数值进行底层寄存器赋值的.结合应用层函数完成spi驱动的代码测试.已达到灵活修改的目的. 按顺 ...

  9. vue单文件组件实例1:简单单文件组件

    ​ HelloWorld.vue: <template> <div class="hello"> <h1>{{msg}}</h1> ...

  10. webservice常用两种身份验证方式

    在项目开发,我们经常会使用WebService,但在使用WebService时我们经常会考虑以下问题:怎么防止别人访问我的WebService?从哪里引用我的WebService?对于第一个问题,就涉 ...