QCustomPlot重写

这篇博客详细探讨了QCustomPlot库的源码,包括QCustomPlot、QCPLayer、QCPLayerable和QCPAxis等核心组件的.h和.cpp文件,深入解析了这些类的实现细节和功能。
摘要由CSDN通过智能技术生成

QCustomPlot

.h

#ifndef QCUSTOMPLOT_H
#define QCUSTOMPLOT_H

#include <QWidget>
class QCPLayer;
class QCPPainter;
class QCPAxis;
class QCustomPlot: public QWidget
{
   
    Q_OBJECT
public:
    explicit QCustomPlot(QWidget *parent = nullptr);
    void replot();
public:
    QCPLayer *layer(const QString &name) const;
    QCPLayer *layer(int index) const;
protected:
    virtual void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
    virtual void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    virtual void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    virtual void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    virtual void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
    virtual void draw(QCPPainter *painter);
private:
    // non-property members:
    QList<QCPLayer*> mLayers;  // 层列表
    QPixmap mPaintBuffer;
    QCPAxis *x1, *x2, *y1, *y2;
};

#endif // QCUSTOMPLOT_H



.cpp

#include "qcustomplot.h"
#include "qcplayer.h"
#include "qcppainter.h"
#include "qcpaxis.h"
#include <QResizeEvent>
#include <QPainter>
#include <QDebug>

QCustomPlot::QCustomPlot(QWidget *parent) : QWidget(parent),
    mPaintBuffer(size())
{
   
    mLayers.append(new QCPLayer(this, QLatin1String("background")));
    mLayers.append(new QCPLayer(this, QLatin1String("axes")));

    x1 = new QCPAxis(QCPAxis::atBottom, this);
    x2 = new QCPAxis(QCPAxis::atTop, this);
    y1 = new QCPAxis(QCPAxis::atLeft, this);
    y2 = new QCPAxis(QCPAxis::atRight, this);

    replot();
}

void QCustomPlot::replot()
{
   
    qDebug() << __FUNCTION__;
    mPaintBuffer.fill(Qt::black);
    QCPPainter painter;
    painter.begin(&mPaintBuffer);
    if (painter.isActive()) {
   
        draw(&painter);
        painter.end();
    }
}

QCPLayer *QCustomPlot::layer(const QString &name) const
{
   
    foreach (QCPLayer *layer, mLayers)
    {
   
        if (layer-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值