当前位置: 首页 > news >正文

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

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

记录一下

qmake .pro文件的配置

QT       += core gui printsupportgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += 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.0SOURCES += \main.cpp \qcustomplot.cpp \widget.cppHEADERS += \qcustomplot.h \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetQMAKE_CXXFLAGS += -Wa,-mbig-obj

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

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

编译通过

在这里插入图片描述

使用示例(代码由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_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic: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.lryc.cn/news/543180.html

相关文章:

  • 【算法】哈希表详解
  • 【红队利器】单文件一键结束火绒6.0
  • Docker小游戏 | 使用Docker部署star-battle太空飞船射击小游戏
  • 【EB-06】SystemCreator dbc转arxml
  • (0)阿里云大模型ACP-考试回忆
  • 按键精灵鹰眼中控:ios多设备管理工具
  • __对于初学者的CCS 汉化
  • JavaScript 系列之:Ajax、Promise、Axios
  • Vidma Ver.2.14.0 高级版
  • Redis Lua Script 溢出漏洞(CVE-2024-31449)
  • 【Mysql】我在广州学Mysql 系列—— 性能优化相关例题
  • java23种设计模式-中介者模式
  • 鸿蒙next 点击穿透实现
  • OpenAPI Generator:API开发的瑞士军刀
  • 某住宅小区地下车库安科瑞的新能源汽车充电桩的配电设计与应用方案 安科瑞 耿笠
  • 电子科技大学考研复习经验分享
  • 2025面试Go真题第一场
  • 【量化策略】趋势跟踪策略
  • leetcode 541. 反转字符串 II 简单
  • org.springframework.boot不存在的其中一个解决办法
  • AI绘画软件Stable Diffusion详解教程(2):Windows系统本地化部署操作方法(专业版)
  • MySql数据库运维学习笔记
  • Linux中Shell运行原理和权限(下)(4)
  • LeetCode热题100- 字符串解码【JavaScript讲解】
  • 每日一题——LRU缓存机制的C语言实现详解
  • Leetcode3162:优质数对的总数 I
  • docker安装etcd:docker离线安装etcd、docker在线安装etcd、etcd镜像下载、etcd配置详解、etcd常用命令、安装常见问题总结
  • Apache SeaTunnel 构建实时数据同步管道(最新版)
  • 递归、搜索与回溯第二讲:二叉树中的深搜 穷举vs暴搜vs深搜vs回溯vs剪枝
  • Hbase分布式——储存机制