点击程序,可以利用鼠标来查看图片细节的东西,下面来看看这个例子吧:
本文链接:
链接:文章的连接 提取码:12dq
界面布局:
途中的界面是重写的类,为了实现鼠标的操作的技巧,自定义一个 QWChartView 类 , 它从 QChartView 继承而来,对鼠标和按键事件进行处理 。
对应的界面代码:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCharts> //必须这么设置
QT_CHARTS_USE_NAMESPACE //必须这么设置
#include <QLabel>
#include "qwchartview.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
QLabel *labXYValue; //状态栏显示文字标签
void createChart(); //创建图表
void prepareData(); //准备数据
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_LegendMarkerClicked(); //图例单击槽函数,自定义槽函数
void on_mouseMovePoint(QPoint point); //鼠标移动事件,自定义槽函数
void on_actZoomReset_triggered(); //工具栏按钮,原始大小
void on_actZoomIn_triggered(); //工具栏按钮,放大
void on_actZoomOut_triggered(); //工具栏按钮,缩小
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
对应的cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qwchartview.h"
void MainWindow::createChart()
{ //创建图表
QChart *chart = new QChart();
// chart->setTitle("简单函数曲线");
// chart->title()
// chart->setAcceptHoverEvents(true);
ui->chartView->setChart(chart);
ui->chartView->setRenderHint(QPainter::Antialiasing);
ui->chartView->setCursor(Qt::CrossCursor); //设置鼠标指针为十字星
QLineSeries *series0 = new QLineSeries();
QLineSeries *series1 = new QLineSeries();
series0->setName("Sin曲线");
series1->setName("Cos曲线");
QPen pen;
pen.setStyle(Qt::DotLine);//Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine
pen.setWidth(2);
pen.setColor(Qt::red);
series0->setPen(pen);
pen.setStyle(Qt::SolidLine);//Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine
pen.setColor(Qt::blue);
series1->setPen(pen);
chart->addSeries(series0);
chart->addSeries(series1);
QValueAxis *axisX = new QValueAxis;
axisX->setRange(0, 10); //设置坐标轴范围
axisX->setLabelFormat("%.1f"); //标签格式
axisX->setTickCount(11); //主分隔个数
axisX->setMinorTickCount(2);//4
axisX->setTitleText("time(secs)"); //标题
// axisX->setGridLineVisible(false);
QValueAxis *axisY = new QValueAxis;
axisY->setRange(-2, 2);
axisY->setTitleText("value");
axisY->setTickCount(5);
axisY->setLabelFormat("%.2f"); //标签格式
// axisY->setGridLineVisible(false);
axisY->setMinorTickCount(2);//4
chart->setAxisX(axisX, series0); //添加X坐标轴
chart->setAxisX(axisX, series1); //添加X坐标轴
chart->setAxisY(axisY, series0); //添加Y坐标轴
chart->setAxisY(axisY, series1); //添加Y坐标轴
// chart->legend()->setAlignment(Qt::AlignRight);
foreach (QLegendMarker* marker, chart->legend()->markers())
{
// QObject::disconnect(marker, SIGNAL(clicked()), this, SLOT(on_LegendMarkerClicked()));
connect(marker, SIGNAL(clicked()), this, SLOT(on_LegendMarkerClicked()));
}
}
void MainWindow::prepareData()
{//为序列生成数据
QLineSeries *series0=(QLineSeries *)ui->chartView->chart()->series().at(0);
QLineSeries *series1=(QLineSeries *)ui->chartView->chart()->series().at(1);
series0->clear();
series1->clear();
qsrand(QTime::currentTime().second());//随机数初始化
qreal t=0,y1,y2,intv=0.1;
qreal rd;
int cnt=100;
for(int i=0;i<cnt;i++)
{
rd=(qrand() % 10)-5; //随机数,-5~+5
y1=qSin(t)+rd/50;//+qrand();
series0->append(t,y1);
rd=(qrand() % 10)-5; //随机数,-5~+5
y2=qSin(t+20)+rd/50;
series1->append(t,y2);
t+=intv;
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setCentralWidget(ui->chartView);
labXYValue = new QLabel("X=, Y= "); //状态栏显示鼠标点的坐标
labXYValue->setMinimumWidth(200);
ui->statusBar->addWidget(labXYValue);
createChart();//创建图表
prepareData();//生成数据
QObject::connect(ui->chartView,SIGNAL(mouseMovePoint(QPoint)),
this, SLOT(on_mouseMovePoint(QPoint))); //鼠标移动事件
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_LegendMarkerClicked()
{//点击图例的marker的响应
QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
switch (marker->type()) //marker的类型
{
case QLegendMarker::LegendMarkerTypeXY: //QLineSeries序列的图例marker
{
marker->series()->setVisible(!marker->series()->isVisible()); //可见性
// marker->setVisible(!marker->isVisible());
marker->setVisible(true);
qreal alpha = 1.0;
if (!marker->series()->isVisible())
alpha = 0.5;
QColor color;
QBrush brush = marker->labelBrush();
color = brush.color();
color.setAlphaF(alpha);
brush.setColor(color);
marker->setLabelBrush(brush);
brush = marker->brush();
color = brush.color();
color.setAlphaF(alpha);
brush.setColor(color);
marker->setBrush(brush);
QPen pen = marker->pen();
color = pen.color();
color.setAlphaF(alpha);
pen.setColor(color);
marker->setPen(pen);
break;
}
default:
break;
}
}
void MainWindow::on_mouseMovePoint(QPoint point)
{ //鼠标移动响应
QPointF pt=ui->chartView->chart()->mapToValue(point); //转换为图表的数值
labXYValue->setText(QString::asprintf("X=%.1f,Y=%.2f",pt.x(),pt.y())); //状态栏显示
}
//void MainWindow::on_chartView_rubberBandChanged(const QRect &viewportRect,
// const QPointF &fromScenePoint, const QPointF &toScenePoint)
//{
Q_UNUSED(fromScenePoint);
Q_UNUSED(toScenePoint);
ui->chartView->rubberBandChanged(viewportRect,fromScenePoint,toScenePoint);
if (ui->chartView->needZoom())
{
ui->chartView->chart()->zoomIn(viewportRect);
ui->chartView->clearNeedZoom();
}
// labXYValue->setText(QString::asprintf("viewportRect(%d,%d,%d,%d)",
// viewportRect.left(),viewportRect.top(),
// viewportRect.width(),viewportRect.height()));
//}
void MainWindow::on_actZoomReset_triggered()
{ //恢复原始大小
ui->chartView->chart()->zoomReset();
}
void MainWindow::on_actZoomIn_triggered()
{//放大
ui->chartView->chart()->zoom(1.2);
}
void MainWindow::on_actZoomOut_triggered()
{//缩小
ui->chartView->chart()->zoom(0.8);
}
下面是新定义的类文件:
qwchartview.h
#ifndef QWCHARTVIEW_H
#define QWCHARTVIEW_H
#include <QtCharts> //必须这么设置
QT_CHARTS_USE_NAMESPACE //必须这么设置
class QWChartView : public QChartView
{
Q_OBJECT
private:
QPoint beginPoint; //选择矩形区的起点
QPoint endPoint; //选择矩形区的终点
protected:
// bool viewportEvent(QEvent *event);
void mousePressEvent(QMouseEvent *event); //鼠标左键按下
void mouseMoveEvent(QMouseEvent *event); //鼠标移动
void mouseReleaseEvent(QMouseEvent *event); //鼠标释放左键
void keyPressEvent(QKeyEvent *event); //按键事件
public:
explicit QWChartView(QWidget *parent = 0);
~QWChartView();
signals:
void mouseMovePoint(QPoint point); //鼠标移动信号,在mouseMoveEvent()事件中触发
};
#endif // QWCHARTVIEW_H
qwchartview.cpp文件:
#include "qwchartview.h"
//QT_CHARTS_USE_NAMESPACE //必须这么设置
#include <QChartView>
//bool QWChartView::viewportEvent(QEvent *event)
//{
if (event->type() == QEvent::TouchBegin) {
// By default touch events are converted to mouse events. So
// after this event we will get a mouse event also but we want
// to handle touch events as gestures only. So we need this safeguard
// to block mouse events that are actually generated from touch.
m_isTouching = true;
// Turn off animations when handling gestures they
// will only slow us down.
chart()->setAnimationOptions(QChart::NoAnimation);
}
// return QChartView::viewportEvent(event);
//}
void QWChartView::mousePressEvent(QMouseEvent *event)
{//鼠标左键按下,记录beginPoint
// QChartView::mousePressEvent(event);
if (event->button()==Qt::LeftButton)
beginPoint=event->pos();
QChartView::mousePressEvent(event);
}
void QWChartView::mouseMoveEvent(QMouseEvent *event)
{//鼠标移动事件
QPoint point;
point=event->pos();
emit mouseMovePoint(point);
QChartView::mouseMoveEvent(event);
}
void QWChartView::mouseReleaseEvent(QMouseEvent *event)
{
// QChartView::mouseReleaseEvent(event);
if (event->button()==Qt::LeftButton)
{ //鼠标左键释放,获取矩形框的endPoint,进行缩放
endPoint=event->pos();
QRectF rectF;
rectF.setTopLeft(this->beginPoint);
rectF.setBottomRight(this->endPoint);
// rectF.setTop(this->chart()->rect().top());
// rectF.setBottom(this->chart()->rect().bottom());
// rectF.setLeft(this->beginPoint.rx());
// rectF.setRight(this->endPoint.rx());
this->chart()->zoomIn(rectF);
}
else if (event->button()==Qt::RightButton)
this->chart()->zoomReset(); //鼠标右键释放,resetZoom
QChartView::mouseReleaseEvent(event);
}
void QWChartView::keyPressEvent(QKeyEvent *event)
{//按键控制
switch (event->key()) {
case Qt::Key_Plus: //+
chart()->zoom(1.2);
break;
case Qt::Key_Minus:
chart()->zoom(0.8);
break;
case Qt::Key_Left:
chart()->scroll(10, 0);
break;
case Qt::Key_Right:
chart()->scroll(-10, 0);
break;
case Qt::Key_Up:
chart()->scroll(0, -10);
break;
case Qt::Key_Down:
chart()->scroll(0, 10);
break;
case Qt::Key_PageUp:
chart()->scroll(0, 50);
break;
case Qt::Key_PageDown:
chart()->scroll(0, -50);
break;
case Qt::Key_Home:
chart()->zoomReset();
// chart()->resetTransform();//没用
// chart()->zoomIn(chart()->plotArea()); //没用
break;
default:
QGraphicsView::keyPressEvent(event);
// break;
}
// QGraphicsView::keyPressEvent(event);
}
QWChartView::QWChartView(QWidget *parent):QChartView(parent)
{
this->setDragMode(QGraphicsView::RubberBandDrag);
// this->setRubberBand(QChartView::RectangleRubberBand);//设置为矩形选择方式
// this->setRubberBand(QChartView::VerticalRubberBand);
// this->setRubberBand(QChartView::HorizontalRubberBand);
this->setMouseTracking(true);//必须开启此功能
}
QWChartView::~QWChartView()
{
}
//QWChartView::QWChartView(QChart *chart, QWidget *parent)
// :QChartView(chart, parent)
//{
// this->setMouseTracking(true);
//}
喜欢我的文章就加个关注吧,以后查找笔记十分的方便哟,一起学习奥利给!