Qt QCustomPlot 图形库详解

原文

QCustomPlot绘图类详解(大白话)

1. 下载qcustomplot.h与qcustomplot.cpp后,将代码文件拷贝到本地工程,并添加到工程项目

在这里插入图片描述

2. 看到文件后就是添加成功了

在这里插入图片描述

3. 在界面中拖拽一个Widget控件,选中并右键选中“提升为”,将原来的Widget控件已成为一个带坐标的 CustomPlot 控件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
如果是代码方式添加就不需要对控件提升的这布操作,直接使用QCustomPlot 类声明对象即可。如

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
   
    ui->setupUi(this);
 
    QCustomPlot *pCustomPlot = new QCustomPlot(this);
    pCustomPlot->resize(300, 300);
}

4. 添加printsupport

如果Qt版本在5.0以上,需要在.pro文件中的QT变量加上printsupport:

QT += widgets printsupport

我是vs开发,所以进行工程配置
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
.cpp

#include "QcustomPlotApplication.h"
#include "ui_QcustomPlotApplication.h"
#include "qcustomplot.h"

//防止中文乱码
//#pragma execution_character_set("utf-8")

#define PT_CNT       200    // 点数
#define GRAPH_CNT    5      // 图数

QcustomPlotApplication::QcustomPlotApplication(QWidget *parent)
    : QMainWindow(parent)
{
   
    ui.setupUi(this);
	InitForm();
	mRefreshCnt = 1;

	//连接信号槽
	connect(ui.btnDynamicDraw,&QPushButton::clicked,this,&QcustomPlotApplication::btnDynamicDraw_clicked);
	connect(ui.btnContinueDraw, &QPushButton::clicked, this, &QcustomPlotApplication::btnContinueDraw_clicked);
}

QcustomPlotApplication::~QcustomPlotApplication()
{
   }

void QcustomPlotApplication::InitForm()
{
   
	//设置图表交互,允许用户通过拖动缩放来与图表进行交互
	//QCP::iRangeDrag允许拖动图表(改变可见范围),而QCP::iRangeZoom允许通过鼠标滚轮或手势缩放图表。
	ui.plotWidget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

#if 0
	//设置背景颜色
	ui.plotWidget->setBackground(QBrush(QColor("#2F4F4F")));

	//设置X轴标签为电流
	ui.plotWidget->xAxis->setLabel(QStringLiteral("电流"));

	//设置Y轴标签为时间(min)
	ui.plotWidget->yAxis->setLabel(QStringLiteral("时间(min)"));

	//设置字体
	QFont plotFont = font();

	//设置字体大小
	plotFont.setPointSizeF(10.0
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值