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

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;
}

相关文章:

  • vue+element ui 实现选择季度组件
  • Linux(CentOS)安装 Nginx
  • java23种设计模式-命令模式
  • 安全性质量属性场景
  • 策略模式结合SpringBoot
  • 银行信贷业务解析:运营与数据双视角下的业务本质与技术支撑
  • C#连接sql server
  • 什么是SEO通俗准确的解释
  • angular轮播图
  • 交换机与路由器连接方式
  • 2.25力扣每日一题--设计内存分配器
  • 排序算法适合的场景
  • TCP,http,WebSocket
  • android aosp系统定制如何监控系统性能
  • 改进的Siddon算法与原算法的区别及具体改进
  • SSL/TLS 协议、SSL证书 和 SSH协议 的区别和联系
  • 中级软考笔记-基础知识-8-网络与信息安全
  • 基于SpringBoot的精品水果线上销售平台系统设计与实现(源码+SQL脚本+LW+部署讲解等)
  • react18自定义hook实现
  • 基于three进行数字孪生平台架构设计及应用实现
  • 网站一直建设中/获客渠道有哪些
  • 263企业邮箱登录登录/自己怎么优化我网站关键词
  • 个人站长还有什么类型的网站可以做/网站seo排名优化软件
  • 网站建设net接口/线上电商怎么做
  • 一般电商都是在哪些网站上做/网站关键词优化推广哪家好
  • 十堰网站设计0719web/在线培训系统app