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

发布于:2025-02-25 ⋅ 阅读:(11) ⋅ 点赞:(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生成,微调了下)

在这里插入图片描述

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