24、Qt使用QCustomPlot

一、下载文件

进入官网,选择“Download”、QCustomPlot.tar.gz

Qt Plotting Widget QCustomPlot - Download

二、创建项目

创建一个"Qt Widget Application"项目,基类选择“QMainWindow”,把刚才下载的压缩包里的“qcustomplot.h”和“qcustomplot.cpp”拷贝到项目目录下

右击项目名称,添加现有文件,选择“qcustomplot.h”和“qcustomplot.cpp”

双击“mainwindow.ui”,往界面上拖拽一个Widget,并进行栅格布局

右击“Widget”,选择“提升为”

填写类名称“QCustomPlot”,点击“添加”

点击“提升”

Widget的基类被更改

三、修改代码

在.pro文件中添加:QT += printsupport

#-------------------------------------------------
#
# Project created by QtCreator 2023-10-04T14:16:44
#
#-------------------------------------------------

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

TARGET = MyCustomPlot
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

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

HEADERS += \
mainwindow.h \
qcustomplot.h

FORMS += \
mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

更改mainwindow.cpp代码入下 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //可移动缩放
    ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom );
    //设置背景颜色
    ui->widget->setBackground(QColor(25,35,45));
    //轴刻度文字
    ui->widget->xAxis->setTickLabelColor(Qt::white);
    ui->widget->yAxis->setTickLabelColor(Qt::white);
    //设定右上角图形标注可见
    ui->widget->legend->setVisible(true);
    ui->widget->legend->setBrush(QColor(25,35,45));
    ui->widget->legend->setTextColor(Qt::white);
    ui->widget->legend->setFont(QFont("Helvetica", 9));
    //设置X轴坐标范围
    ui->widget->xAxis->setRange(-10, 100);
    //设置Y轴坐标范围
    ui->widget->yAxis->setRange(-150, 150);
    ui->widget->addGraph();
    ui->widget->graph(0)->setName("通道1");
    ui->widget->graph(0)->setPen(QPen(QColor(178,34,34)));
    //传入数据 QVector<double>类型
    QVector<double> xData;
    QVector<double> yData;
    for(int i = 0; i < 100; i++)
    {
        xData.append(i);
        yData.append(150 * sin(i));
    }
    ui->widget->graph(0)->setData(xData, yData);
}

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

四、运行测试

运行程序,界面显示如下

相关推荐

  1. QT C++ QCustomPlot 简单使用

    2023-12-28 23:50:03       29 阅读
  2. QML使用QCustomPlot笔记

    2023-12-28 23:50:03       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 23:50:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 23:50:03       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 23:50:03       87 阅读
  4. Python语言-面向对象

    2023-12-28 23:50:03       96 阅读

热门阅读

  1. 【哲学 学习笔记01】哲学家及其思想汇总

    2023-12-28 23:50:03       54 阅读
  2. lftp学习笔记

    2023-12-28 23:50:03       70 阅读
  3. 字符串拼接js

    2023-12-28 23:50:03       57 阅读
  4. 将anaconda3的虚拟环境安装至指定位置

    2023-12-28 23:50:03       47 阅读
  5. 二、C#基础语法( 变量与数据类型)

    2023-12-28 23:50:03       58 阅读
  6. 鹏城杯2023初赛 Reverse WriteUp

    2023-12-28 23:50:03       115 阅读
  7. 用户表格及筛选表单配置 - PC通用管理模块(1)

    2023-12-28 23:50:03       69 阅读
  8. vue defineAsyncComponent 异步加载组件

    2023-12-28 23:50:03       60 阅读