#include "zchartview.h"
ZChartView::ZChartView(ZChart *chart, QWidget *parent):
QChartView (chart,parent)
{
connect(chart,&ZChart::showPointRectSign,this,&ZChartView::showPointRectSlots);
setRubberBand(QChartView::RectangleRubberBand); //设置橡皮筋
setRenderHint(QPainter::Antialiasing); //设置平滑曲线
setStyleSheet("background:transparent");
QPen pen; // 定义一个画笔,设置画笔颜色和宽度
pen.setColor("red");
pen.setWidth(1);
x_line = new QGraphicsLineItem(); // 定义一个直线图元
x_line->setLine(QLineF(10, 0, 10, this->height()));
x_line->setPen(pen);
x_line->setEnabled(false);
x_line->setVisible(false);
scene()->addItem(x_line);
y_line = new QGraphicsLineItem(); // 定义一个直线图元
y_line->setLine(QLineF(10, 0, 10, this->height()));
y_line->setPen(pen);
y_line->setEnabled(false);
y_line->setVisible(false);
scene()->addItem(y_line);
//添加文字背景框矩形
currentPointTextRect = new QGraphicsRectItem(0 , 0 ,100, 60);
currentPointTextRect->setBrush(Qt::yellow);
currentPointTextRect->setEnabled(false);
currentPointTextRect->setVisible(false);
scene()->addItem(currentPointTextRect);
currentPointText=new QGraphicsTextItem(currentPointTextRect);
currentPointText->setHtml("");
currentPointText->setEnabled(false);
scene()->addItem(currentPointText);
}
ZChartView::~ZChartView()
{
qDebug()<<" 调用了ZChartView的析构函数 ";
}
void ZChartView::mouseMoveEvent(QMouseEvent *e)
{
//打印鼠标位置处的点的坐标值
// qDebug()<<"鼠标移入点位"<
chart()->mapToValue(e->pos());//把鼠标坐标值转化为画出的图中的坐标
//以防父类的实例接收不到mouseMoveEvent事件。
QChartView::mouseMoveEvent(e);//调用父类的重写方法。本行程序不同于e->ignore(),ignor是把事件继续向父控件(一般其父控件就是ui界面)传递,而不是向父类传递
x_line->setLine(e->x(),0,e->x(),this->height());
y_line->setLine(0,e->y(),this->width(),e->y());
currentPointTextRect->setPos(e->pos());
currentPointText->setHtml(" "+QString::number(this->chart()->mapToValue(e->pos()).x())+"
"+QString::number(this->chart()->mapToValue(e->pos()).y())+"
");
currentPointTextRect->setPos(e->pos());
}
void ZChartView::enterEvent(QEvent *event)
{
x_line->setVisible(true);
y_line->setVisible(true);
currentPointTextRect->setVisible(true);
}
void ZChartView::leaveEvent(QEvent *event)
{
x_line->setVisible(false);
y_line->setVisible(false);
currentPointTextRect->setVisible(false);
}
void ZChartView::showPointRectSlots(QPointF point, bool state)
{
// if(state)
// {
// currentPointTextRect->setVisible(true);
// currentPointText->setHtml(" "+QString::number(point.x())+"
"+QString::number(point.y())+"
");
// currentPointTextRect->setPos(this->chart()->mapToPosition(point));
// }
// else
// {
// currentPointTextRect->setVisible(false);
// }
}