【项目-】Qt + QCustomPlot 实现频谱监测仪:四图联动、高频信号注入、鼠标交互全解析

四个联动图表(main1/main2 联动,sub3/sub4 联动)

高频信号由外部接口实时传入(X, Y 坐标)

鼠标跟随竖线 + 点击显示坐标

黑底紫轴绿柱的视觉风格

使用 QSplitter 实现可拖动调整大小的 2x2 布局

兼容 Qt Designer UI 文件

项目结构

SpectrumMonitor/

├── SpectrumMonitor.pro

├── main.cpp

├── mainwindow.h

├── mainwindow.cpp

├── mainwindow.ui

├── qcustomplot.h

└── qcustomplot.cpp

  1. .pro 文件
    pro
cpp 复制代码
QT       += core gui widgets
CONFIG   += c++17
TARGET    = SpectrumMonitor
TEMPLATE  = app

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

HEADERS  += mainwindow.h \
            qcustomplot.h

FORMS    += mainwindow.ui

#关键:包含当前目录

INCLUDEPATH += $$PWD

cpp 复制代码
 2. main.cpp
cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    // 模拟外部高频信号注入(实际项目中由检测算法调用)
    QTimer::singleShot(1500, &w, [&]() {
        w.addHighFreqSignal(250.0, 120.0); // X=250MHz, Y=120
    });
    QTimer::singleShot(3000, &w, [&]() {
        w.addHighFreqSignal(320.5, 95.0);  // X=320.5MHz, Y=95
    });

    return a.exec();
}
cpp 复制代码
 mainwindow.h
cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCustomPlot>
#include <QCPItemText>
#include <vector>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    // 外部接口:注入高频信号 (X: MHz, Y: 幅度)
    void addHighFreqSignal(double x, double y);

protected:
    bool eventFilter(QObject* obj, QEvent* event) override;

private slots:
    void updateSignalPlots(); // 定时器回调

private:
    void setupPlots();
    void setupSplitter();
    void setupTimers();
    void updateGroupFollowLine(QCustomPlot* plot, double xCoord);
    void clearGroupFollowLines(QCustomPlot* plot);
    double generateSignalPoint(int i);

    Ui::MainWindow *ui;
    QCPItemText* coordLabel;
    QTimer* m_plotTimer;

    // 高频信号队列 (X, Y)
    std::vector<std::pair<double, double>> 

```cpp
m_highFreqQueue;
};
#endif // MAINWINDOW_H
复制代码
 4. mainwindow.cpp

```cpp
cpp
#include "mainwindow.h"
            obj == ui->sub3Plot || obj == ui->sub4Plot) {
            auto e = static_cast<QMouseEvent*>(event);
            QCustomPlot* plot = qobject_cast<QCustomPlot*>(obj);
            double x = plot->xAxis->pixelToCoord(e->pos().x());
            double y = plot->yAxis->pixelToCoord(e->pos().y());
            coordLabel->setText(QString("%1 %2").arg(x, 0, 'f', 1).arg(y, 0, 'f', 2));
            coordLabel->position->setCoords(x, y);
            coordLabel->setVisible(true);
            plot->replot();
            return true;
        }
    }
    return QMainWindow::eventFilter(obj, event);
}

void MainWindow::updateGroupFollowLine(QCustomPlot* plot, double xCoord) {
    // 清除旧线
    for (int i = plot->itemCount() - 1; i >= 0; --i) {
        if (plot->item(i)->inherits("QCPItemLine")) {
            plot->removeItem(plot->item(i));
        }
    }

    // 设置 Y 范围
    double yMax = (plot == ui->main1Plot || plot == ui->sub3Plot) ? 300 : 100;

    // 创建新线
    QCPItemLine* line = new QCPItemLine(plot);
    line->start->setCoords(xCoord, 0);
    line->end->setCoords(xCoord, yMax);
    line->setPen(QPen(Qt::red, 1, Qt::DashLine));
    plot->replot();
}

void MainWindow::clearGroupFollowLines(QCustomPlot* plot) {
    for (int i = plot->itemCount() - 1; i >= 0; --i) {
        if (plot->item(i)->inherits("QCPItemLine")) {
            plot->removeItem(plot->item(i));
        }
    }
    coordLabel->setVisible(false);
    plot->replot();
}
  1. mainwindow.ui 设计要点
    拖入 4 个 QWidget
    提升(Promote)为 QCustomPlot:
    右键 QWidget → Promote to...
    Base class: QWidget
    Promoted class: QCustomPlot
    Header file: qcustomplot.h
    设置 objectName:
    main1Plot, main2Plot, sub3Plot, sub4Plot
    注意:UI 文件中不要设置布局!布局由 setupSplitter() 代码控制。

使用说明

将 qcustomplot.h/.cpp 放在项目根目录

在 Qt Designer 中提升 4 个 QWidget 为 QCustomPlot

编译运行

外部系统调用 addHighFreqSignal(x, y) 注入信号

相关推荐
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner2 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz7 天前
QML Hello World 入门示例
qt
xcyxiner10 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner11 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner11 天前
DicomViewer (添加模型类)3
qt
xcyxiner12 天前
DicomViewer (目录调整) 2
qt
xcyxiner12 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00614 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术14 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript