# 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. cocos 水果机,老Tiger虎机流水灯,博彩大转盘效果

    原(http://www.cnblogs.com/zisou/p/cocos2d-xZhuanpan.html) 博彩大转盘,转盘抽奖的小系统,这是一个很有意思的游戏模块,游戏中增加这样一些趣味的小模 ...

  2. .htaccess使用方法介绍

    1..htaccess文件使用前提 .htaccess的主要作用就是实现url改写,也就是当浏览器通过url访问到服务器某个文件夹时,作为主人,我们可以来接待这个url,具体地怎样接待它,就是此文件的 ...

  3. windows----------Windows10 远程桌面连接失败,报CredSSP加密oracle修正错误解决办法

    1.通过运行gpedit.msc进入组策略配置(需要win10专业版,家庭版无解),策略路径:“计算机配置”->“管理模板”->“系统”->“凭据分配”,设置名称: 加密 Oracl ...

  4. spring boot中使用@Async实现异步调用任务

    本篇文章主要介绍了spring boot中使用@Async实现异步调用任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 什么是“异步调用”? “异步调用”对应的是“同步 ...

  5. 统计Oracle一个表空间中各个segment占用的空间大小

    SQL语句参考以下: select owner,segment_name,segment_type,tablespace_name,sum(bytes)/1024/1024/1024 from dba ...

  6. windows 中的类似于sudo的命令(在cmd中以另一个用户的身份运行命令)

    linux中我们习惯用sudo命令来临时用另一个用户的身份执行命令. windows中,通常我们需要用管理员权限执行命令的时候通常是 右键->run as administrator. 用着键盘 ...

  7. Django框架详细介绍---请求流程

    Django请求流程图 1.客户端发送请求 2.wsgiref是Django封装的套接字,它将客户端发送过来的请求(请求头.请求体封装成request) 1)解析请求数据 2)封装响应数据 3.中间件 ...

  8. vue配置手机通过IP访问电脑开发环境

    vue配置手机通过IP访问电脑开发环境config/index.js// Various Dev Server settings host: '0.0.0.0', // can be overwrit ...

  9. PHP中文转拼音

    网上大都讲的,都不支持繁体字,毕竟就是一个函数解决的事. 推荐一个很好的扩展,github地址: https://github.com/overtrue/pinyin 怎么用,自己去看就行了.

  10. 第十四节 JS面向对象基础

    什么是面向对象:在不需要知道它内部结构和原理的情况下,能够有效的使用它,比如,电视.洗衣机等也可以被定义为对象 什么是对象:在Java中对象就是“类的实体化”,在JavaScript中基本相同:对象是 ...