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

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

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生成,微调了下)

在这里插入图片描述

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.kler.cn/a/560904.html

相关文章:

  • SSL和TLS:深入了解网络安全的基石
  • 【DeepSeek与鸿蒙HarmonyOS:开启应用开发新次元】
  • DSP芯片C6678的SRIO及其中断跳转的配置
  • LeetCode 热题 100 94. 二叉树的中序遍历
  • 基于SpringBoot的“流浪动物救助系统”的设计与实现(源码+数据库+文档+PPT)
  • Redis中集合(Set)常见命令详解
  • MySQL 主从集群同步延迟问题分析与解决方案
  • Transformer LLaMA
  • Qt在Linux嵌入式开发过程中复杂界面滑动时卡顿掉帧问题分析及解决方案
  • 部署若依微服务遇到的坑
  • 被AWS反撸了,试一下能否申请退还
  • AWS EC2加速型计算实例全解析:从vt1到p5,如何为AI算力选择最佳引擎?
  • qt:多元素类,容器类,布局类
  • 基于Docker的前端环境管理:从开发环境到生产部署的实现方案
  • Rancher-产品架构
  • 2.3 变量
  • 基于大数据技术智能教学系统的设计与实现
  • 深入浅出ES6:现代JavaScript的基石
  • XML DOM4J 二、document对象
  • 离线环境如何玩转LLM?Ollama一键部署指南(Ubuntu)