QT QcustomPlot的简单使用
第一步、QcustomPlot是QT提供的一个第三方库,在使用前需要在QcustomPlot官网上进行下载。
第二步、把解压完的QcustomPlot压缩包中的qcustomplot.h和qcustomplot.cpp文件添加到工程文件中来。使用时应先在源文件处点击添加现有文件,把这两个文件添加进来。


第三步、打开UI界面,把weiget控件添加到界面里,然后右键点击控件,选择提升

在提升的类名上写QcustomPlot,最后点击提升即可。
这样QcustomPlot这个第三方库就可以使用了。
以下是一简单的曲线代码。
.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTime>
#include <QDebug> MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//设置鼠标点击精度
ui->customPlot->setSelectionTolerance(); for(int i=;i<;i++)
{
num[i]=;
}
n=;
QTimer *t = new QTimer(this);
t->start();
connect(t,SIGNAL(timeout()),this,SLOT(graph_show()));
connect(ui->customPlot,SIGNAL(mouseRelease(QMouseEvent*)),this,SLOT(mouseReleaseEvent(QMouseEvent*)));
//connect(tracer,SIGNAL(mouseMove(QMouseEvent*)),this,SLOT(mouseMoveEvent(QMouseEvent*))); } MainWindow::~MainWindow()
{
delete ui;
} void MainWindow::graph_show()
{
n += PI/;
graph_show(ui->customPlot);
} void MainWindow::graph_show(QCustomPlot *customPlot)
{ QVector<double> x(),y();
for(int i=;i<;i++)
{
num[i]=num[i+];
}
num[]=n;
for(int i=;i<;i++)
{
x[i] = i;
y[i] = sin(num[i]);
}
//添加一条曲线
customPlot->addGraph();
//设置曲线的颜色
customPlot->graph()->setPen(QPen(Qt::red));
//给曲线传递两个参数
customPlot->graph()->setData(x,y);
//给曲线的横纵坐标命名
customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");
//设置横纵坐标的范围
customPlot->xAxis->setRange(,);
customPlot->yAxis->setRange(-,);
//进行曲线重画
customPlot->replot();
/*
customPlot->setInteraction(QCP::iRangeZoom,true);
customPlot->axisRect()->setRangeDrag(Qt::Vertical);
customPlot->setInteraction(QCP::iRangeDrag,true);
*/
} void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
//排除非左鼠标键
if (e->button() != Qt::LeftButton)
{
return;
} //获取点击的点坐标
QPointF ChickedPoint = e->pos();
//排除区间外鼠标点
if(!ui->customPlot->viewport().contains(e->pos()))
{
return;
}
//将像素坐标转换为轴值
double currentx = ui->customPlot->xAxis->pixelToCoord(ChickedPoint.x());
double currenty = ui->customPlot->yAxis->pixelToCoord(ChickedPoint.y());
//使用QToolTip输出值,
QToolTip::showText(mapToGlobal(e->pos()),QString("当前点值为:x=%1,y=%2").arg(currentx).arg(currenty),this);
}
.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include "ui_mainwindow.h"
#include <QMouseEvent> #define PI 3.1415926 namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow();
//设置一容器
double num[];
double n=;
void graph_show(QCustomPlot *customPlot); public slots:
void graph_show();
void mouseReleaseEvent(QMouseEvent *e);
// void mouseMoveEvent(QMouseEvent *e); private:
Ui::MainWindow *ui;
}; #endif // MAINWINDOW_H
静态曲线的命名方法可以选用:
customPlot->legend->setVisible(true);
customPlot->graph()->setName("sin");
此处是对第一条曲线进行命名为“sin“。
QT QcustomPlot的简单使用的更多相关文章
- QT 选择对话框简单示例
QT 选择对话框简单示例 部分代码: pDialog->addSeparator(); QAction *pmb2 = pDialog->addAction(QString::fromLo ...
- 使用QT实现一个简单的登陆对话框(纯代码实现C++)
使用QT实现一个简单的登陆对话框(纯代码实现C++) 效果展示 使用的QT控件 控件 描述 QLable 标签 QLineEdit 行文本框 QPushButton 按扭 QHBoxLayout 水平 ...
- 使用qt制作一个简单的计算器
前言:今天使用qt制作了一个很简单的计算器,觉得挺有意思的,所以在这里跟大家分享一下. 这里先跟大家说说使用到的函数: 一.槽连接函数 connect(信号发送者,发送的信号,信号接收者,信号接收者的 ...
- 9、Qt Project之简单的数据库接口
简单的数据库接口 Step1:首先完成整个UI界面的额设计: <?xml version="1.0" encoding="UTF-8"?> < ...
- 如何用Qt Python创建简单的桌面条形码应用
Qt for Python可以快速跨平台的GUI应用.这篇文章分享下如何结合Dynamsoft Barcode Reader SDK来创建一个简单的读码应用. 安装Qt for Python 官方站点 ...
- 使用qt写的简单的图片浏览器
功能特别简单,支持png,jpg,bmp,gif文件,支持自适应窗口大小,支持放大缩小,旋转功能还有点问题,支持上下按键选择图片 因为初学qt,所以很多东西都不太会,而且c++学的不是太好,没有怎么使 ...
- Qt实现一个简单的TextEditor
使用QT实现简单的TextEditor: 首先在窗口添加部件TextEditor,并设置中文字符 MainWindow::MainWindow(QWidget *parent) : QMainWind ...
- QT QcustomPlot的使用(二)
在QcustomPlot中,给横纵坐标添加箭头的方法 //在末尾添加箭头 customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeA ...
- 用Qt写的简单屏保程序
近日老大提别人家产品都有屏保程序,貌似我们也该有,简单在qtcn.org请教了一下,写了个小程序! 晕倒,半天没找到上传功能!我已经上传到qtcn上了,地址如下: http://www.qtcn.or ...
随机推荐
- c 字符串的结束标志
//char carray[]="nihao"; char carray[]={'a','b','c','d','\0'}; printf("array=%s" ...
- centos7 安装anaconda3
在学习Python的时候,如果学习python3的版本,那么需要重新安装pytho3,因为系统默认的Python版本是python2,当然,有的可能需要同时保留两个版本,这里来试着使用一下anacon ...
- CSS 交集选择器和并集选择器
交集选择器是and 也就是要同时满足 且只能交2个只能交2个只能交2个,第一个是标记,第二个是class或者id,之间不可以有空格 eg: span.small-height 并集选择器是or,也就 ...
- 上下文对象-请求对象-响应对象(ServletContext - response - request)
目录 上下文对象-请求对象-响应对象 ServletContext response request 上下文对象-请求对象-响应对象 ServletContext 什么是ServletContext ...
- 服务器(Linux)上运行python总结
跑实验换了几次服务器了,每次遇到相似问题都要重新百度,而且每次百度搜索出的顺序都不一样,又得重新找半天,这次把遇到的问题都总结一下. 1.准备 PuTTY和FileZilla FileZilla使用F ...
- 字节组数(二进制流)、Base64、图片(文件)、二进制相互之间转换
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...
- Linux中强大的top命令
top命令算是最直观.好用的查看服务器负载的命令了.它实时动态刷新显示服务器状态信息,且可以通过交互式命令自定义显示内容,非常强大. 在终端中输入top,回车后会显示如下内容: top - 21: ...
- bad object refs/remotes/origin/HEAD
How to handle git gc fatal: bad object refs/remotes/origin/HEAD error: failed to run repack I random ...
- 问题:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile)
一:问题 今天编译maven 项目构建失败,提示内容如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler ...
- Windows10下Anaconda虚拟环境下安装pycocotools
0 - 步骤 安装visualcppbuildtools_full.exe(链接:https://blog.csdn.net/u012247418/article/details/82314129) ...