Qt 小例子学习42 - qcustomplot 边界、填充渐变

Qt 小例子学习42 - qcustomplot 边界、填充渐变

#include "qcustomplot.h"
#include <QApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QCustomPlot *_myPlot = new QCustomPlot;
    _myPlot->resize(640, 480);

    // some data
    QVector<double> x;
    QVector<double> y;

    for (int point = 0; point < 100; point++)
    {
        x << point;
        y << 80 * qSin(point * 0.1);
    }

    QCPGraph *newCurve = new QCPGraph(_myPlot->xAxis, _myPlot->yAxis);
    newCurve->addData(x, y);

    QBrush shadowBrush(QColor(0, 0, 0), Qt::Dense7Pattern);

    newCurve->setBrush(shadowBrush);

    QCPGraph *minGraph = new QCPGraph(_myPlot->xAxis, _myPlot->yAxis);
    newCurve->setChannelFillGraph(minGraph);

    QObject::connect(
        _myPlot->xAxis,
        static_cast<void (QCPAxis::*)(const QCPRange &)>(&QCPAxis::rangeChanged),
        [_myPlot, minGraph](const QCPRange & newRange)
    {
        minGraph->setData(QVector<double> {newRange.lower, newRange.upper},
                          QVector<double> {_myPlot->yAxis->range().lower,
                                           _myPlot->yAxis->range().lower
                                          });
    });

    QObject::connect(
        _myPlot->yAxis,
        static_cast<void (QCPAxis::*)(const QCPRange &)>(&QCPAxis::rangeChanged),
        [_myPlot, minGraph](const QCPRange & newRange)
    {
        minGraph->setData(QVector<double> {_myPlot->xAxis->range().lower,
                                           _myPlot->xAxis->range().upper
                                          },
                          QVector<double> {newRange.lower, newRange.lower});
    });

    // test

    _myPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom |
                             QCP::iSelectAxes | QCP::iSelectLegend |
                             QCP::iSelectPlottables);
    QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, [&_myPlot]()
    {
        QCPRange range = _myPlot->yAxis->range();
        range.lower -= 1;
        _myPlot->yAxis->setRange(range);
        _myPlot->replot();
    });

    timer.start(100);
    _myPlot->rescaleAxes();
    _myPlot->show();
    return a.exec();
}

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值