栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

33、图表的放大缩小、移动等操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

33、图表的放大缩小、移动等操作


samp9_4.pro
#-------------------------------------------------
#
# Project created by QtCreator 2016-12-27T13:17:47
#
#-------------------------------------------------

QT       += core gui

QT       += charts

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = samp9_4
TEMPLATE = app


SOURCES += main.cpp
        mainwindow.cpp 
    qwchartview.cpp

HEADERS  += mainwindow.h 
    qwchartview.h

FORMS    += mainwindow.ui

RESOURCES += 
    res.qrc

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

#include      //必须这么设置
QT_CHARTS_USE_NAMESPACE     //必须这么设置
#include    

#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

mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include    "qwchartview.h"

void MainWindow::createChart()
{ //创建图表
    QChart *chart = new QChart();
    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

//下面4行代码在Qt 5.12.1中编译时提示过时,
//    chart->setAxisX(axisX, series0); //添加X坐标轴
//    chart->setAxisX(axisX, series1); //添加X坐标轴
//    chart->setAxisY(axisY, series0); //添加Y坐标轴
//    chart->setAxisY(axisY, series1); //添加Y坐标轴

//另外一种添加坐标轴的方式,Qt 5.12.1中编译没问题
    chart->addAxis(axisX,Qt::AlignBottom); //坐标轴添加到图表,并指定方向
    chart->addAxis(axisY,Qt::AlignLeft);

    series0->attachAxis(axisX); //序列 series0 附加坐标轴
    series0->attachAxis(axisY);

    series1->attachAxis(axisX); //序列 series1 附加坐标轴
    series1->attachAxis(axisY);

//    chart->legend()->setAlignment(Qt::AlignRight);
    foreach (QLegendMarker* marker, chart->legend()->markers())
    {
        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
        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 (sender());

    switch (marker->type()) //marker的类型
    {
        case QLegendMarker::LegendMarkerTypeXY: //QLineSeries序列的图例marker
        {
            marker->series()->setVisible(!marker->series()->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_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);
}

mainwindow.ui


 MainWindow
 
  
   
    0
    0
    729
    474
   
  
  
   
    10
   
  
  
   鼠标选择缩放与数值显示
  
  
   
    
     
      50
      10
      611
      331
     
    
    
     QGraphicsView::RubberBandDrag
    
   
  
  
   
    
     0
     0
     729
     23
    
   
  
  
   
    Qt::ToolButtonTextBesideIcon
   
   
    TopToolBarArea
   
   
    false
   
   
   
   
   
  
  
  
   
    
     :/images/images/414.bmp:/images/images/414.bmp
   
   
    原始大小
   
   
    原始大小
   
  
  
   
    
     :/images/images/132.bmp:/images/images/132.bmp
   
   
    退出
   
   
    退出
   
  
  
   
    
     :/images/images/418.bmp:/images/images/418.bmp
   
   
    放大
   
   
    放大
   
  
  
   
    
     :/images/images/416.bmp:/images/images/416.bmp
   
   
    缩小
   
   
    缩小
   
  
 
 
 
  
   QWChartView
   QGraphicsView
   
qwchartview.h
actQuit triggered() MainWindow close() -1 -1 339 206
qwchartview.h
#ifndef QWCHARTVIEW_H
#define QWCHARTVIEW_H


#include      //必须这么设置
QT_CHARTS_USE_NAMESPACE     //必须这么设置


class QWChartView : public QChartView
{
    Q_OBJECT

private:
    QPoint  beginPoint; //选择矩形区的起点
    QPoint  endPoint;  //选择矩形区的终点

protected:
    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"

#include    


void QWChartView::mousePressEvent(QMouseEvent *event)
{//鼠标左键按下,记录beginPoint
    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)
{
    if (event->button()==Qt::LeftButton)
    { //鼠标左键释放,获取矩形框的endPoint,进行缩放
        endPoint=event->pos();
        QRectF  rectF;
        rectF.setTopLeft(this->beginPoint);
        rectF.setBottomRight(this->endPoint);
        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();
        break;
    default:
        QGraphicsView::keyPressEvent(event);
    }
//    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()
{

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966210.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号