C++ QT 6.6.1 QCustomPlot的导入及使用注意事项和示例 | 关于高版本QT使用QCustomPlot报错问题解决的办法

news/2025/2/26 5:15:47

QCustomPlot__QTQCustomPlot_0">C++ QT 6.6.1 QCustomPlot的导入及使用注意事项和示例 | 关于高版本QT使用QCustomPlot报错问题解决的办法

记录一下

qmake .pro文件的配置

QT       += core gui printsupport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

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

HEADERS += \
    qcustomplot.h \
    widget.h

FORMS += \
    widget.ui

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

QMAKE_CXXFLAGS += -Wa,-mbig-obj

关键在于这两条(debug模式编译通过不报错)

一个是添加printsupport,另一个是解决太大不能编译的问题
QT       += core gui printsupport
QMAKE_CXXFLAGS += -Wa,-mbig-obj

编译通过

在这里插入图片描述

DeepSeek_41">使用示例(代码由DeepSeek生成,微调了下)

在这里插入图片描述

widget.h文件
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>
#include <QElapsedTimer>
#include <QRandomGenerator>
#include "qcustomplot.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
private slots:
    void realtimeDataSlot();
private:
    QCustomPlot *customPlot;
    QTimer dataTimer;
    double xOffset = 0;
    QElapsedTimer timer;
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp文件
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 初始化图表
    customPlot = new QCustomPlot(this);
    customPlot->setGeometry(0,0,width(),height());
    // 设置图表标题
    customPlot->plotLayout()->insertRow(0);
    customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(customPlot, "实时数据演示", QFont("微软雅黑", 12, QFont::Bold)));
    // 添加数据曲线
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(QColor(40, 110, 255))); // 蓝色线条
    customPlot->graph(0)->setName("正弦波形");
    customPlot->graph(0)->setBrush(QColor(40, 110, 255, 20)); // 半透明填充
    // 配置坐标轴
    customPlot->xAxis->setLabel("时间 (s)");
    customPlot->yAxis->setLabel("数值");
    customPlot->xAxis->setRange(0, 10);
    customPlot->yAxis->setRange(-1.5, 1.5);
    // 显示图例
    customPlot->legend->setVisible(true);
    customPlot->legend->setFont(QFont("微软雅黑", 9));
    // 设置定时器用于实时更新数据
    connect(&dataTimer, &QTimer::timeout, this, &Widget::realtimeDataSlot);
    dataTimer.start(50); // 每50ms更新一次
    timer.start();
}

Widget::~Widget()
{
    delete ui;
}
void Widget::realtimeDataSlot()
{
    // 计算新数据点
    double key = timer.elapsed()/1000.0; // 时间戳(秒)
    static double lastPointKey = 0;
    if (key - lastPointKey > 0.002) // 添加数据间隔2ms
    {
        // 添加正弦波数据
        double value = sin(key + xOffset);
        // 添加数据到曲线
        customPlot->graph(0)->addData(key, value);
        // 使x轴向右滚动
        customPlot->xAxis->setRange(key, 10, Qt::AlignRight);
        // 重绘图表
        customPlot->replot();
        lastPointKey = key;
    }
    // 随机改变相位用于演示效果
     xOffset += (QRandomGenerator::global()->generateDouble() - 0.5) * 0.02;
}

http://www.niftyadmin.cn/n/5868006.html

相关文章

反向代理模块kfj

1 概念 1.1 反向代理概念 反向代理是指以代理服务器来接收客户端的请求&#xff0c;然后将请求转发给内部网络上的服务器&#xff0c;将从服务器上得到的结果返回给客户端&#xff0c;此时代理服务器对外表现为一个反向代理服务器。 对于客户端来说&#xff0c;反向代理就相当于…

ros面试准备

ROS中的通信方式有哪些&#xff1f; topic service action topic:发布-订阅模型&#xff0c;适合持续的数据流&#xff0c;如传感器 service:请求-响应模型&#xff0c;适合即时操作&#xff0c;如开关控制 如何调试一个无法通信的话题&#xff1f; 第一、rostopic list检查话…

TDengine 产品组件:taosKeeper

taosKeeper 是 TDengine 3.0 版本监控指标的导出工具&#xff0c;通过简单的几项配置即可获取 TDengine 的运行状态。taosKeeper 使用 TDengine RESTful 接口&#xff0c;所以不需要安装 TDengine 客户端即可使用。 安装 taosKeeper 有两种安装方式&#xff1a; 安装 TDengin…

java23种设计模式-抽象工厂模式

抽象工厂模式&#xff08;Abstract Factory Pattern&#xff09;学习笔记 &#x1f31f; 定义 抽象工厂模式属于创建型设计模式&#xff0c;提供一个创建一系列相关或相互依赖对象的接口&#xff0c;而无需指定它们具体的类。是工厂方法模式的升级版&#xff0c;支持多个产品…

【idea问题排查技巧】

以下是针对 IDEA 中 日志打标(动态标记) 和 全链路追踪 功能的分步详解,结合具体场景和操作截图说明,帮助快速掌握实战技巧。 一、动态日志打标:不修改代码输出关键信息 1. 断点日志打印(非侵入式打标) 场景:在调试时,需要临时查看某个变量的值,但不想修改代码添加…

在Spring Boot中如何使用Freemaker模板引擎

在 Spring Boot 中使用 FreeMarker 模板引擎可以帮助你创建动态的 Web 页面。以下是详细的步骤和示例代码,介绍如何在 Spring Boot 项目里集成和使用 FreeMarker。 1. 添加依赖 如果你使用的是 Maven 项目,需要在 pom.xml 文件中添加 FreeMarker 相关依赖。Spring Boot 提供…

Elasticsearch索引设计与分片策略深度优化-手记

一、索引设计的黄金法则&#xff08;从踩坑到精通的必经之路&#xff09; 1. 字段类型显式声明原则 动态映射是新手最易踩的坑&#xff0c;某金融平台曾因金额字段被自动识别为text类型&#xff0c;导致聚合查询时触发OOM。正确做法应显式声明核心字段&#xff1a; PUT /fin…

虚拟机中如何调整宿主机的交换空间设置

1. 增加交换空间 创建交换文件&#xff1a;例如创建一个 4GB 的交换文件。 sudo fallocate -l 4G /swapfile2. 设置交换文件权限 sudo chmod 600 /swapfile3. 将文件格式化为交换空间 sudo mkswap /swapfile4. 启用交换空间 sudo swapon /swapfile5. 使交换空间永久生效 …