QT的qcustomplot(二维画图)

讲解都在注释中 

qcustomplot.pro

#-------------------------------------------------
#
# Project created by QtCreator 2018-03-09T14:50:00
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport

TARGET = qcustomplot
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    qcustomplot.cpp

HEADERS  += mainwindow.h \
    qcustomplot.h

FORMS    += mainwindow.ui

main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindows.cpp

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


#define CanvasBGColor_0     QColor(80, 80, 80)//画布背景颜色
#define CanvasBGColor_1     QColor(50, 50, 50)
#define CoorBGColor_0       QColor(80, 80, 80)  //坐标背景颜色
#define CoorBGColor_1       QColor(50, 50, 50)
#define TextColor           QColor(255,255,255)
#define Plot_NoColor        QColor(0,0,0,0)

#define Plot_DotColor       QColor(236,110,0)
#define Plot_LineColor      QColor(180,252,253)
#define Plot_BGColor        QColor(246,98,0,80)

#define TextWidth   1
#define LineWidth   2
#define DotWidth    10

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->resize(800, 600);
    initPlot();
    setupPlot();
}


void MainWindow::initPlot()
{
    //设置四边都有坐标
    ui->plot->axisRect()->setupFullAxesBox(true);

    //设置坐标颜色/坐标名称颜色
    ui->plot->yAxis->setLabelColor(TextColor);
    ui->plot->xAxis->setLabelColor(TextColor);
    ui->plot->xAxis->setTickLabelColor(TextColor);
    ui->plot->yAxis->setTickLabelColor(TextColor);
    ui->plot->xAxis->setBasePen(QPen(TextColor, TextWidth));
    ui->plot->yAxis->setBasePen(QPen(TextColor, TextWidth));
    ui->plot->xAxis->setTickPen(QPen(TextColor, TextWidth));
    ui->plot->yAxis->setTickPen(QPen(TextColor, TextWidth));
    ui->plot->xAxis->setSubTickPen(QPen(TextColor, TextWidth));
    ui->plot->yAxis->setSubTickPen(QPen(TextColor, TextWidth));
    ui->plot->yAxis2->setLabelColor(TextColor);
    ui->plot->xAxis2->setLabelColor(TextColor);
    ui->plot->xAxis2->setTickLabelColor(TextColor);
    ui->plot->yAxis2->setTickLabelColor(TextColor);
    ui->plot->xAxis2->setBasePen(QPen(TextColor, TextWidth));
    ui->plot->yAxis2->setBasePen(QPen(TextColor, TextWidth));
    ui->plot->xAxis2->setTickPen(QPen(TextColor, TextWidth));
    ui->plot->yAxis2->setTickPen(QPen(TextColor, TextWidth));
    ui->plot->xAxis2->setSubTickPen(QPen(TextColor, TextWidth));
    ui->plot->yAxis2->setSubTickPen(QPen(TextColor, TextWidth));

    ui->plot->xAxis->setAutoTickStep(false);
    ui->plot->xAxis->setTickStep(60);
    ui->plot->yAxis->setAutoTickStep(false);
    ui->plot->yAxis->setTickStep(60);
    // 设置坐标轴名称
    ui->plot->xAxis->setLabel("x");
    ui->plot->yAxis->setLabel("y");
    //设置可拖拽
    ui->plot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom);
    //将网格的线显示出来
    ui->plot->xAxis->grid()->setSubGridVisible(true);
    ui->plot->yAxis->grid()->setSubGridVisible(true);
    //设置坐标的分度值
    QCPAxisRect *subRectLeft = new QCPAxisRect(ui->plot, false); // false means to not setup default axes
    QCPAxisRect *subRectRight = new QCPAxisRect(ui->plot, false);
    subRectLeft->addAxes(QCPAxis::atBottom | QCPAxis::atLeft);
    subRectRight->addAxes(QCPAxis::atBottom | QCPAxis::atRight);
    subRectLeft->axis(QCPAxis::atLeft)->setAutoTickCount(2);
    subRectRight->axis(QCPAxis::atRight)->setAutoTickCount(2);
    subRectRight->axis(QCPAxis::atBottom)->setAutoTickCount(2);
    subRectLeft->axis(QCPAxis::atBottom)->grid()->setVisible(true);
    //设置画布背景色
    QLinearGradient plotGradient;
    plotGradient.setStart(0, 0);
    plotGradient.setFinalStop(0, 350);
    plotGradient.setColorAt(0, CanvasBGColor_0);
    plotGradient.setColorAt(1, CanvasBGColor_1);
    ui->plot->setBackground(plotGradient);

    //设置坐标背景色
    QLinearGradient axisRectGradient;
    axisRectGradient.setStart(0, 0);
    axisRectGradient.setFinalStop(0, 350);
    axisRectGradient.setColorAt(0, CoorBGColor_0);
    axisRectGradient.setColorAt(1, CoorBGColor_1);
    ui->plot->axisRect()->setBackground(axisRectGradient);

    // 设置坐标轴显示范围,否则只能看到默认范围
    ui->plot->xAxis->setRange(0, 480);
    ui->plot->yAxis->setRange(0, 480);
}


void MainWindow::setupPlot()
{
    ui->plot->addGraph(); // blue dot
    ui->plot->graph(0)->setPen(QPen(Qt::red));
    ui->plot->graph(0)->setLineStyle(QCPGraph::lsNone);
    ui->plot->graph(0)->setScatterStyle(QCPScatterStyle::ssDisc);
    ui->plot->graph(0)->addData(0, 0);
    ui->plot->replot();
}


MainWindow::~MainWindow()
{
    delete ui;
}

你可能感兴趣的:(QT)