QCustomPlot使用范例(一)

QCustomPlot是一个基于Qt中的一个的图形库,用于绘制各种图示,并为实时可视化应用程序提供高性能服务。

QCustomPlot可以导出为各种格式,比如:PDF文件和位图(如:PNG、JPG、BMP)。

可在自己的项目中直接使用两个源文件(qcustomplot.h与qcustomplot.cpp),或预先编译成库。

下载地址:https://www.qcustomplot.com/

使用方法:

下载之后文件夹中有qcustomplot.h和qcustomplot.cpp。

在TQ中新建好工程之后,只需要分别导入这两个文件即可。当然,也可以采用动态库等方式导入QCustomPlot库。

在工程文件.pro中添加QCustomPlot:

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport  #添加printsupport

TARGET = readCor
TEMPLATE = app

画图示例代码:

mainwindow.cpp文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include <QColor>
#include <QVector>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("Test by fc");

    QCustomPlot *customPlot = new QCustomPlot(this);  //创建一个QCustomPlot对象
    customPlot->addGraph();  //添加一条曲线
    customPlot->graph(0)->setPen(QPen(Qt::blue));    //设置曲线颜色
    customPlot->graph(0)->setBrush(QBrush(QColor(0,0,255,20)));//设置填充色
    customPlot->graph(0)->setName("一");      // 设置曲线图的名字

    customPlot->addGraph();
    customPlot->graph(1)->setPen(QPen(Qt::red));
    customPlot->graph(1)->setName("二");      // 设置曲线图的名字

    QVector<double>x(251),y0(251),y1(251);
    for (int i=0; i<251; ++i)
    {
      x[i] = i;
      y0[i] = qExp(-i/150.0)*qCos(i/10.0);
      y1[i] = qExp(-i/150.0);
    }

    /*
     * 暂时不知道具体作用
        customPlot->xAxis2->setVisible(true);
        customPlot->xAxis2->setTickLabels(false);
        customPlot->yAxis2->setVisible(true);
        customPlot->yAxis2->setTickLabels(false);
        // make left and bottom axes always transfer their ranges to right and top axes:
        connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
        connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
    */

    customPlot->graph(0)->setData(x, y0); //设置数据
    customPlot->graph(1)->setData(x, y1); //设置数据
    customPlot->graph(0)->rescaleAxes(true); //缩放范围
    customPlot->graph(1)->rescaleAxes(true);//缩放范围
    // Note: we could have also just called customPlot->rescaleAxes(); instead
    // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

    customPlot->xAxis->setRange(-200, 400);           // 设置x轴的范围
    customPlot->yAxis->setRange(-1, 1);          // 设置y轴的范围

    customPlot->resize(600, 600);        // 设置总的大小
    customPlot->xAxis->setLabel("x");                  // 设置x轴的标签
    customPlot->yAxis->setLabel("y");                // 设置y轴的标签

   // customPlot->setBackground(QColor(0,0,0));// 设置背景色
    customPlot->legend->setVisible(true);             // 显示图例
   // customPlot->savePng("customPlot.png", 400, 300);
}


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


结果展示:

这只是根据官方文档中修改的一个小例子,其它的还有很多例子,按照这样的形式可以直接使用。具体见:qcustomplot官方首页

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`CompletableFuture`是Java 8中引入的一个非常强大的工具,用于处理异步计算和并行执行。它提供了一种线程安全的方式来处理异步操作的结果,简化了回调地狱。以下是一个简单的范例,展示如何使用`CompletableFuture`执行异步任务: ```java import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class CompletableFutureExample { public static void main(String[] args) throws ExecutionException, InterruptedException { // 创建一个异步任务 CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { System.out.println("开始计算"); // 模拟耗时操作,这里我们只是简单休眠 Thread.sleep(2000); return "异步任务的结果"; }); // 主线程继续执行,不会阻塞 System.out.println("主线程正在执行其他任务..."); // 当异步任务完成后,获取结果 String result = future.get(); // 或者用future.thenApply(result -> ...) System.out.println("最终结果: " + result); } } ``` 在这个例子中: 1. `CompletableFuture.supplyAsync()`方法创建了一个异步任务,它在后台线程中执行,返回一个`CompletableFuture`对象。 2. `Thread.sleep(2000)`模拟了一个延迟执行的任务,实际应用中可能是数据库查询、网络请求等耗时操作。 3. `future.get()`会阻塞,直到异步任务完成并获取结果。如果不希望阻塞,可以使用`thenApply()`、`thenAccept()`或`thenRun()`等方法来处理结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值