#include "mainwindow.h"
#include "ui_Mainwindow.h"
#include "qcustomplot.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 创建主工具栏
mainToolBar = new QToolBar("主工具栏", this);
mainToolBar->setMovable(false); // 禁止工具栏移动
addToolBar(Qt::TopToolBarArea, mainToolBar);
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setPen(QPen(Qt::blue)); // 线条颜色为蓝色
// 生成一些数据点
QVector<double> x(251), y0(251);
for (int i=0; i<251; ++i)
{
x[i] = i;
y0[i] = qExp(-i/150.0)*qCos(i/10.0); // 指数衰减的余弦函数
}
// 配置右轴和上轴显示刻度但不显示标签
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->xAxis2->setTickLabels(false);
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->yAxis2->setTickLabels(false);
// 使左轴和下轴的范围变化同步到右轴和上轴
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
// 将数据点传递给图形
ui->customPlot->graph(0)->setData(x, y0);
// 自动调整坐标轴范围以适应图形
ui->customPlot->graph(0)->rescaleAxes();
// 允许用户通过鼠标拖动坐标轴范围、使用鼠标滚轮缩放以及通过点击选择图形
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
// 计算图表中心位置
double xCenter = (ui->customPlot->xAxis->range().lower + ui->customPlot->xAxis->range().upper) / 2;
double yCenter = (ui->customPlot->yAxis->range().lower + ui->customPlot->yAxis->range().upper) / 2;
// 初始化第一组纵向和横向光标线
verticalLine1 = new QCPItemStraightLine(ui->customPlot);
verticalLine1->setPen(QPen(Qt::red));
verticalLine1->point1->setCoords(0, ui->customPlot->yAxis->range().lower);
verticalLine1->point2->setCoords(0, ui->customPlot->yAxis->range().upper);
verticalLine1->setVisible(false);
horizontalLine1 = new QCPItemStraightLine(ui->customPlot);
horizontalLine1->setPen(QPen(Qt::red));
horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, 0);
horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, 0);
horizontalLine1->setVisible(false);
// 初始化第一组标签
verticalLabel1 = new QCPItemText(ui->customPlot);
verticalLabel1->setLayer("overlay"); // 确保标签显示在最上层
verticalLabel1->setClipToAxisRect(false);
verticalLabel1->setPositionAlignment(Qt::AlignBottom|Qt::AlignHCenter);
verticalLabel1->position->setType(QCPItemPosition::ptAxisRectRatio);
verticalLabel1->position->setCoords(0, 0); // 初始位置
verticalLabel1->setText("Time: 0");
verticalLabel1->setFont(QFont(font().family(), 9));
verticalLabel1->setPen(QPen(Qt::red));
verticalLabel1->setBrush(QBrush(Qt::white));
verticalLabel1->setVisible(false);
horizontalLabel1 = new QCPItemText(ui->customPlot);
horizontalLabel1->setLayer("overlay");
horizontalLabel1->setClipToAxisRect(false);
horizontalLabel1->setPositionAlignment(Qt::AlignLeft|Qt::AlignVCenter);
horizontalLabel1->position->setType(QCPItemPosition::ptAxisRectRatio);
horizontalLabel1->position->setCoords(0, 0); // 初始位置
horizontalLabel1->setText("Voltage: 0");
horizontalLabel1->setFont(QFont(font().family(), 9));
horizontalLabel1->setPen(QPen(Qt::red));
horizontalLabel1->setBrush(QBrush(Qt::white));
horizontalLabel1->setVisible(false);
// 初始化第二组纵向和横向光标线
verticalLine2 = new QCPItemStraightLine(ui->customPlot);
verticalLine2->setPen(QPen(Qt::green));
// verticalLine2->point1->setCoords(0, ui->customPlot->yAxis->range().lower);
// verticalLine2->point2->setCoords(0, ui->customPlot->yAxis->range().upper);
verticalLine2->point1->setCoords(xCenter + 10, ui->customPlot->yAxis->range().lower);
verticalLine2->point2->setCoords(xCenter + 10, ui->customPlot->yAxis->range().upper);
verticalLine2->setVisible(false);
horizontalLine2 = new QCPItemStraightLine(ui->customPlot);
horizontalLine2->setPen(QPen(Qt::green));
// horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, 0);
// horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, 0);
horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, yCenter + 0.2);
horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, yCenter + 0.2);
horizontalLine2->setVisible(false);
// 初始化第二组标签
verticalLabel2 = new QCPItemText(ui->customPlot);
verticalLabel2->setLayer("overlay"); // 确保标签显示在最上层
verticalLabel2->setClipToAxisRect(false);
verticalLabel2->setPositionAlignment(Qt::AlignBottom|Qt::AlignHCenter);
verticalLabel2->position->setType(QCPItemPosition::ptAxisRectRatio);
verticalLabel2->position->setCoords(0, 0); // 初始位置
verticalLabel2->setText("Time: 0");
verticalLabel2->setFont(QFont(font().family(), 9));
verticalLabel2->setPen(QPen(Qt::green));
verticalLabel2->setBrush(QBrush(Qt::white));
verticalLabel2->setVisible(false);
horizontalLabel2 = new QCPItemText(ui->customPlot);
horizontalLabel2->setLayer("overlay");
horizontalLabel2->setClipToAxisRect(false);
horizontalLabel2->setPositionAlignment(Qt::AlignLeft|Qt::AlignVCenter);
horizontalLabel2->position->setType(QCPItemPosition::ptAxisRectRatio);
horizontalLabel2->position->setCoords(0, 0); // 初始位置
horizontalLabel2->setText("Voltage: 0");
horizontalLabel2->setFont(QFont(font().family(), 9));
horizontalLabel2->setPen(QPen(Qt::green));
horizontalLabel2->setBrush(QBrush(Qt::white));
horizontalLabel2->setVisible(false);
// 创建差值标签
diffLabel = new QCPItemText(ui->customPlot);
diffLabel->setLayer("overlay");
diffLabel->setClipToAxisRect(false);
diffLabel->setPositionAlignment(Qt::AlignTop|Qt::AlignHCenter);
diffLabel->position->setType(QCPItemPosition::ptAxisRectRatio);
diffLabel->position->setCoords(0.5, 0.05); // 顶部中间位置
diffLabel->setFont(QFont(font().family(), 10, QFont::Bold));
diffLabel->setPen(QPen(Qt::black));
diffLabel->setBrush(QBrush(Qt::white));
diffLabel->setVisible(false);
// 初始化状态
isVerticalDragging1 = false;
isHorizontalDragging1 = false;
isVerticalDragging2 = false;
isHorizontalDragging2 = false;
cursorEditMode = false;
verticalLine1Created = false;
horizontalLine1Created = false;
verticalLine2Created = false;
horizontalLine2Created = false;
// 设置光标模式动作
cursorModeAction = new QAction("光标模式", this);
cursorModeAction->setIcon(QIcon(":/images/line_icon/rewind-button.png"));
cursorModeAction->setCheckable(true);
mainToolBar->addAction(cursorModeAction);
connect(cursorModeAction, &QAction::triggered, this, &MainWindow::on_cursorModeAction_triggered);
ui->statusbar->showMessage("光标编辑模式已关闭");
// 连接鼠标移动事件到 mouseMoveEvent 槽函数
connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMoveEvent(QMouseEvent*)));
connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePressEvent(QMouseEvent*)));
connect(ui->customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(mouseReleaseEvent(QMouseEvent*)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_cursorModeAction_triggered()
{
cursorEditMode = cursorModeAction->isChecked();
if (cursorEditMode) {
ui->customPlot->setInteractions(QCP::iSelectPlottables);
ui->statusbar->showMessage("光标编辑模式已开启 - 左键添加/移动垂直光标,右键添加/移动水平光标");
} else {
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
ui->statusbar->showMessage("光标编辑模式已关闭");
verticalLine1->setVisible(false);
horizontalLine1->setVisible(false);
verticalLabel1->setVisible(false);
horizontalLabel1->setVisible(false);
verticalLine2->setVisible(false);
horizontalLine2->setVisible(false);
verticalLabel2->setVisible(false);
horizontalLabel2->setVisible(false);
diffLabel->setVisible(false);
verticalLine1Created = false;
horizontalLine1Created = false;
verticalLine2Created = false;
horizontalLine2Created = false;
activeVerticalLine = nullptr;
activeVerticalLabel = nullptr;
activeHorizontalLine = nullptr;
activeHorizontalLabel = nullptr;
}
ui->customPlot->replot();
}
// void MainWindow::mousePressEvent(QMouseEvent *event)
// {
// if (cursorEditMode) {
// double x = ui->customPlot->xAxis->pixelToCoord(event->x());
// double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// // 检查点击是否靠近第一组垂直光标
// if (qAbs(x - verticalLine1->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
// isVerticalDragging1 = true;
// verticalLine1->setVisible(true);
// verticalLabel1->setVisible(true);
// verticalLine1Created = true;
// }
// // 检查点击是否靠近第一组水平光标
// if (qAbs(y - horizontalLine1->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
// isHorizontalDragging1 = true;
// horizontalLine1->setVisible(true);
// horizontalLabel1->setVisible(true);
// horizontalLine1Created = true;
// }
// // 检查点击是否靠近第二组垂直光标
// if (qAbs(x - verticalLine2->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
// isVerticalDragging2 = true;
// verticalLine2->setVisible(true);
// verticalLabel2->setVisible(true);
// verticalLine2Created = true;
// }
// // 检查点击是否靠近第二组水平光标
// if (qAbs(y - horizontalLine2->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
// isHorizontalDragging2 = true;
// horizontalLine2->setVisible(true);
// horizontalLabel2->setVisible(true);
// horizontalLine2Created = true;
// }
// // 如果点击在空白处,根据鼠标位置设置新光标位置
// if (event->button() == Qt::LeftButton) {
// if (!verticalLine1Created) {
// verticalLine1->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine1->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// verticalLine1->setVisible(true);
// verticalLabel1->setVisible(true);
// isVerticalDragging1 = true;
// verticalLine1Created = true;
// } else if (!verticalLine2Created) {
// verticalLine2->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine2->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// verticalLine2->setVisible(true);
// verticalLabel2->setVisible(true);
// isVerticalDragging2 = true;
// verticalLine2Created = true;
// }
// } else if (event->button() == Qt::RightButton) {
// if (!horizontalLine1Created) {
// horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// horizontalLine1->setVisible(true);
// horizontalLabel1->setVisible(true);
// isHorizontalDragging1 = true;
// horizontalLine1Created = true;
// } else if (!horizontalLine2Created) {
// horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// horizontalLine2->setVisible(true);
// horizontalLabel2->setVisible(true);
// isHorizontalDragging2 = true;
// horizontalLine2Created = true;
// }
// }
// ui->customPlot->replot();
// } else {
// QMainWindow::mousePressEvent(event);
// }
// }
// void MainWindow::mousePressEvent(QMouseEvent *event)
// {
// if (cursorEditMode) {
// double x = ui->customPlot->xAxis->pixelToCoord(event->x());
// double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// // 检查点击是否靠近垂直光标
// if (qAbs(x - verticalLine1->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
// activeVerticalLine = verticalLine1;
// activeVerticalLabel = verticalLabel1;
// isVerticalDragging1 = true;
// verticalLine1->setVisible(true);
// verticalLabel1->setVisible(true);
// verticalLine1Created = true;
// } else if (qAbs(x - verticalLine2->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
// activeVerticalLine = verticalLine2;
// activeVerticalLabel = verticalLabel2;
// isVerticalDragging2 = true;
// verticalLine2->setVisible(true);
// verticalLabel2->setVisible(true);
// verticalLine2Created = true;
// }
// // 检查点击是否靠近水平光标
// if (qAbs(y - horizontalLine1->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
// activeHorizontalLine = horizontalLine1;
// activeHorizontalLabel = horizontalLabel1;
// isHorizontalDragging1 = true;
// horizontalLine1->setVisible(true);
// horizontalLabel1->setVisible(true);
// horizontalLine1Created = true;
// } else if (qAbs(y - horizontalLine2->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
// activeHorizontalLine = horizontalLine2;
// activeHorizontalLabel = horizontalLabel2;
// isHorizontalDragging2 = true;
// horizontalLine2->setVisible(true);
// horizontalLabel2->setVisible(true);
// horizontalLine2Created = true;
// }
// // 如果点击在空白处,根据鼠标位置设置新光标位置
// if (event->button() == Qt::LeftButton) {
// if (!verticalLine1Created) {
// activeVerticalLine = verticalLine1;
// activeVerticalLabel = verticalLabel1;
// verticalLine1->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine1->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// verticalLine1->setVisible(true);
// verticalLabel1->setVisible(true);
// isVerticalDragging1 = true;
// verticalLine1Created = true;
// } else if (!verticalLine2Created) {
// activeVerticalLine = verticalLine2;
// activeVerticalLabel = verticalLabel2;
// verticalLine2->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine2->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// verticalLine2->setVisible(true);
// verticalLabel2->setVisible(true);
// isVerticalDragging2 = true;
// verticalLine2Created = true;
// }
// } else if (event->button() == Qt::RightButton) {
// if (!horizontalLine1Created) {
// activeHorizontalLine = horizontalLine1;
// activeHorizontalLabel = horizontalLabel1;
// horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// horizontalLine1->setVisible(true);
// horizontalLabel1->setVisible(true);
// isHorizontalDragging1 = true;
// horizontalLine1Created = true;
// } else if (!horizontalLine2Created) {
// activeHorizontalLine = horizontalLine2;
// activeHorizontalLabel = horizontalLabel2;
// horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// horizontalLine2->setVisible(true);
// horizontalLabel2->setVisible(true);
// isHorizontalDragging2 = true;
// horizontalLine2Created = true;
// }
// }
// ui->customPlot->replot();
// } else {
// QMainWindow::mousePressEvent(event);
// }
// }
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (cursorEditMode) {
double x = ui->customPlot->xAxis->pixelToCoord(event->x());
double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// 重置拖动状态
isVerticalDragging1 = false;
isHorizontalDragging1 = false;
isVerticalDragging2 = false;
isHorizontalDragging2 = false;
// 检查垂直光标(仅左键处理)
if (event->button() == Qt::LeftButton) {
if (verticalLine1Created && qAbs(x - verticalLine1->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
isVerticalDragging1 = true;
} else if (verticalLine2Created && qAbs(x - verticalLine2->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
isVerticalDragging2 = true;
}
}
// 检查水平光标(仅右键处理)
if (event->button() == Qt::RightButton) {
if (horizontalLine1Created && qAbs(y - horizontalLine1->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
isHorizontalDragging1 = true;
} else if (horizontalLine2Created && qAbs(y - horizontalLine2->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
isHorizontalDragging2 = true;
}
}
// 如果点击在空白处,根据鼠标位置设置新光标位置
if (event->button() == Qt::LeftButton) {
if (!verticalLine1Created) {
verticalLine1->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
verticalLine1->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
verticalLine1->setVisible(true);
verticalLabel1->setVisible(true);
isVerticalDragging1 = true;
verticalLine1Created = true;
// 更新标签文本
//verticalLabel1->setText(QString("Time: %1").arg(x, 0, 'f', 2));
updateVerticalCursorLabel(verticalLabel1, x);
} else if (!verticalLine2Created) {
verticalLine2->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
verticalLine2->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
verticalLine2->setVisible(true);
verticalLabel2->setVisible(true);
isVerticalDragging2 = true;
verticalLine2Created = true;
// 更新标签文本
//verticalLabel1->setText(QString("Time: %1").arg(x, 0, 'f', 2));
updateVerticalCursorLabel(verticalLabel2, x);
}
} else if (event->button() == Qt::RightButton) {
if (!horizontalLine1Created) {
horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
horizontalLine1->setVisible(true);
horizontalLabel1->setVisible(true);
isHorizontalDragging1 = true;
horizontalLine1Created = true;
// 更新标签文本
//horizontalLabel1->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
updateHorizontalCursorLabel(horizontalLabel1, y);
} else if (!horizontalLine2Created) {
horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
horizontalLine2->setVisible(true);
horizontalLabel2->setVisible(true);
isHorizontalDragging2 = true;
horizontalLine2Created = true;
// 更新标签文本
//horizontalLabel1->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
updateHorizontalCursorLabel(horizontalLabel2, y);
}
}
ui->customPlot->replot();
} else {
QMainWindow::mousePressEvent(event);
}
}
// void MainWindow::mouseMoveEvent(QMouseEvent *event)
// {
// if (cursorEditMode) {
// double x = ui->customPlot->xAxis->pixelToCoord(event->x());
// double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// if (isVerticalDragging1) {
// verticalLine1->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine1->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// // 更新垂直标签位置和内容
// verticalLabel1->position->setCoords(
// (x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(),
// 1.0
// );
// verticalLabel1->setText(QString("Time: %1").arg(x, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Time: %1").arg(x, 0, 'f', 2));
// }
// if (isHorizontalDragging1) {
// horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// // 更新水平标签位置和内容
// horizontalLabel1->position->setCoords(
// 0.0,
// (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size()
// );
// horizontalLabel1->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Voltage: %1").arg(y, 0, 'f', 2));
// }
// if (isVerticalDragging2) {
// verticalLine2->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine2->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// // 更新垂直标签位置和内容
// verticalLabel2->position->setCoords(
// (x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(),
// 1.0
// );
// verticalLabel2->setText(QString("Time: %1").arg(x, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Time: %1").arg(x, 0, 'f', 2));
// }
// if (isHorizontalDragging2) {
// horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// // 更新水平标签位置和内容
// horizontalLabel2->position->setCoords(
// 0.0,
// (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size()
// );
// horizontalLabel2->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Voltage: %1").arg(y, 0, 'f', 2));
// }
// ui->customPlot->replot();
// calculateAndDisplayDifferences();
// } else {
// QMainWindow::mouseMoveEvent(event);
// }
// }
// void MainWindow::mouseMoveEvent(QMouseEvent *event)
// {
// if (cursorEditMode) {
// double x = ui->customPlot->xAxis->pixelToCoord(event->x());
// double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// if (isVerticalDragging1 || isVerticalDragging2) {
// activeVerticalLine->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// activeVerticalLine->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// // 更新垂直标签位置和内容
// activeVerticalLabel->position->setCoords(
// (x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(),
// 1.0
// );
// activeVerticalLabel->setText(QString("Time: %1").arg(x, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Time: %1").arg(x, 0, 'f', 2));
// }
// if (isHorizontalDragging1 || isHorizontalDragging2) {
// activeHorizontalLine->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// activeHorizontalLine->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// // 更新水平标签位置和内容
// activeHorizontalLabel->position->setCoords(
// 0.0,
// (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size()
// );
// activeHorizontalLabel->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Voltage: %1").arg(y, 0, 'f', 2));
// }
// ui->customPlot->replot();
// calculateAndDisplayDifferences();
// } else {
// QMainWindow::mouseMoveEvent(event);
// }
// }
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if (cursorEditMode) {
double x = ui->customPlot->xAxis->pixelToCoord(event->x());
double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// 独立处理垂直光标拖动
if (isVerticalDragging1) {
// verticalLine1->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine1->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
verticalLine1->point1->setCoords(x, verticalLine1->point1->coords().y());
verticalLine1->point2->setCoords(x, verticalLine1->point2->coords().y());
verticalLabel1->position->setCoords(
(x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(), 1.0);
verticalLabel1->setText(QString("Time: %1").arg(x, 0, 'f', 2));
} else if (isVerticalDragging2) {
// verticalLine2->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine2->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
verticalLine2->point1->setCoords(x, verticalLine2->point1->coords().y());
verticalLine2->point2->setCoords(x, verticalLine2->point2->coords().y());
verticalLabel2->position->setCoords(
(x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(), 1.0);
verticalLabel2->setText(QString("Time: %1").arg(x, 0, 'f', 2));
}
// 独立处理水平光标拖动
if (isHorizontalDragging1) {
// horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
horizontalLine1->point1->setCoords(horizontalLine1->point1->coords().x(), y);
horizontalLine1->point2->setCoords(horizontalLine2->point2->coords().x(), y);
horizontalLabel1->position->setCoords(
0.0, (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size());
horizontalLabel1->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
} else if (isHorizontalDragging2) {
// horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
horizontalLine2->point1->setCoords(horizontalLine2->point1->coords().x(), y);
horizontalLine2->point2->setCoords(horizontalLine2->point2->coords().x(), y);
horizontalLabel2->position->setCoords(
0.0, (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size());
horizontalLabel2->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
}
ui->customPlot->replot();
calculateAndDisplayDifferences();
qDebug() << "Creating horizontal line 2 at y:" << y;
} else {
QMainWindow::mouseMoveEvent(event);
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
isVerticalDragging1 = false;
isHorizontalDragging1 = false;
isVerticalDragging2 = false;
isHorizontalDragging2 = false;
if (!cursorEditMode) {
QMainWindow::mouseReleaseEvent(event);
}
}
void MainWindow::calculateAndDisplayDifferences()
{
static double lastTimeDiff = 0;
static double lastVoltageDiff = 0;
QString statusMessage;
bool needsUpdate = false;
// 计算并显示时间差值
if (verticalLine1->visible() && verticalLine2->visible()) {
double timeDiff = qAbs(verticalLine1->point1->coords().x() - verticalLine2->point1->coords().x());
if (!qFuzzyCompare(timeDiff, lastTimeDiff)) {
statusMessage += QString("ΔT: %1 ").arg(timeDiff, 0, 'f', 2);
lastTimeDiff = timeDiff;
needsUpdate = true;
}
}
// 计算并显示电压差值
if (horizontalLine1->visible() && horizontalLine2->visible()) {
double voltageDiff = qAbs(horizontalLine1->point1->coords().y() - horizontalLine2->point1->coords().y());
if (!qFuzzyCompare(voltageDiff, lastVoltageDiff)) {
statusMessage += QString("ΔV: %1").arg(voltageDiff, 0, 'f', 2);
lastVoltageDiff = voltageDiff;
needsUpdate = true;
}
}
// 仅在需要时更新UI
if (needsUpdate) {
ui->statusbar->showMessage(statusMessage);
if (diffLabel) {
diffLabel->setText(statusMessage);
diffLabel->setVisible(!statusMessage.isEmpty());
}
ui->customPlot->replot(QCustomPlot::rpQueuedReplot);
}
}
void MainWindow::updateVerticalCursorLabel(QCPItemText *label, double x)
{
label->position->setCoords(
(x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(), 1.0);
label->setText(QString("Time: %1").arg(x, 0, 'f', 2));
}
void MainWindow::updateHorizontalCursorLabel(QCPItemText *label, double y)
{
label->position->setCoords(
0.0, (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size());
label->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
qDebug() << "Setting horizontal label 2 text to voltage:" << y;
}
#include "mainwindow.h"
#include "ui_Mainwindow.h"
#include "qcustomplot.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 创建主工具栏
mainToolBar = new QToolBar("主工具栏", this);
mainToolBar->setMovable(false); // 禁止工具栏移动
addToolBar(Qt::TopToolBarArea, mainToolBar);
ui->customPlot->addGraph();
ui->customPlot->graph(0)->setPen(QPen(Qt::blue)); // 线条颜色为蓝色
// 生成一些数据点
QVector<double> x(251), y0(251);
for (int i=0; i<251; ++i)
{
x[i] = i;
y0[i] = qExp(-i/150.0)*qCos(i/10.0); // 指数衰减的余弦函数
}
// 配置右轴和上轴显示刻度但不显示标签
ui->customPlot->xAxis2->setVisible(true);
ui->customPlot->xAxis2->setTickLabels(false);
ui->customPlot->yAxis2->setVisible(true);
ui->customPlot->yAxis2->setTickLabels(false);
// 使左轴和下轴的范围变化同步到右轴和上轴
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
// 将数据点传递给图形
ui->customPlot->graph(0)->setData(x, y0);
// 自动调整坐标轴范围以适应图形
ui->customPlot->graph(0)->rescaleAxes();
// 允许用户通过鼠标拖动坐标轴范围、使用鼠标滚轮缩放以及通过点击选择图形
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
// 计算图表中心位置
double xCenter = (ui->customPlot->xAxis->range().lower + ui->customPlot->xAxis->range().upper) / 2;
double yCenter = (ui->customPlot->yAxis->range().lower + ui->customPlot->yAxis->range().upper) / 2;
// 初始化第一组纵向和横向光标线
verticalLine1 = new QCPItemStraightLine(ui->customPlot);
verticalLine1->setPen(QPen(Qt::red));
verticalLine1->point1->setCoords(0, ui->customPlot->yAxis->range().lower);
verticalLine1->point2->setCoords(0, ui->customPlot->yAxis->range().upper);
verticalLine1->setVisible(false);
horizontalLine1 = new QCPItemStraightLine(ui->customPlot);
horizontalLine1->setPen(QPen(Qt::red));
horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, 0);
horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, 0);
horizontalLine1->setVisible(false);
// 初始化第一组标签
verticalLabel1 = new QCPItemText(ui->customPlot);
verticalLabel1->setLayer("overlay"); // 确保标签显示在最上层
verticalLabel1->setClipToAxisRect(false);
verticalLabel1->setPositionAlignment(Qt::AlignBottom|Qt::AlignHCenter);
verticalLabel1->position->setType(QCPItemPosition::ptAxisRectRatio);
verticalLabel1->position->setCoords(0, 0); // 初始位置
verticalLabel1->setText("Time: 0");
verticalLabel1->setFont(QFont(font().family(), 9));
verticalLabel1->setPen(QPen(Qt::red));
verticalLabel1->setBrush(QBrush(Qt::white));
verticalLabel1->setVisible(false);
horizontalLabel1 = new QCPItemText(ui->customPlot);
horizontalLabel1->setLayer("overlay");
horizontalLabel1->setClipToAxisRect(false);
horizontalLabel1->setPositionAlignment(Qt::AlignLeft|Qt::AlignVCenter);
horizontalLabel1->position->setType(QCPItemPosition::ptAxisRectRatio);
horizontalLabel1->position->setCoords(0, 0); // 初始位置
horizontalLabel1->setText("Voltage: 0");
horizontalLabel1->setFont(QFont(font().family(), 9));
horizontalLabel1->setPen(QPen(Qt::red));
horizontalLabel1->setBrush(QBrush(Qt::white));
horizontalLabel1->setVisible(false);
// 初始化第二组纵向和横向光标线
verticalLine2 = new QCPItemStraightLine(ui->customPlot);
verticalLine2->setPen(QPen(Qt::green));
// verticalLine2->point1->setCoords(0, ui->customPlot->yAxis->range().lower);
// verticalLine2->point2->setCoords(0, ui->customPlot->yAxis->range().upper);
verticalLine2->point1->setCoords(xCenter + 10, ui->customPlot->yAxis->range().lower);
verticalLine2->point2->setCoords(xCenter + 10, ui->customPlot->yAxis->range().upper);
verticalLine2->setVisible(false);
horizontalLine2 = new QCPItemStraightLine(ui->customPlot);
horizontalLine2->setPen(QPen(Qt::green));
// horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, 0);
// horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, 0);
horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, yCenter + 0.2);
horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, yCenter + 0.2);
horizontalLine2->setVisible(false);
// 初始化第二组标签
verticalLabel2 = new QCPItemText(ui->customPlot);
verticalLabel2->setLayer("overlay"); // 确保标签显示在最上层
verticalLabel2->setClipToAxisRect(false);
verticalLabel2->setPositionAlignment(Qt::AlignBottom|Qt::AlignHCenter);
verticalLabel2->position->setType(QCPItemPosition::ptAxisRectRatio);
verticalLabel2->position->setCoords(0, 0); // 初始位置
verticalLabel2->setText("Time: 0");
verticalLabel2->setFont(QFont(font().family(), 9));
verticalLabel2->setPen(QPen(Qt::green));
verticalLabel2->setBrush(QBrush(Qt::white));
verticalLabel2->setVisible(false);
horizontalLabel2 = new QCPItemText(ui->customPlot);
horizontalLabel2->setLayer("overlay");
horizontalLabel2->setClipToAxisRect(false);
horizontalLabel2->setPositionAlignment(Qt::AlignLeft|Qt::AlignVCenter);
horizontalLabel2->position->setType(QCPItemPosition::ptAxisRectRatio);
horizontalLabel2->position->setCoords(0, 0); // 初始位置
horizontalLabel2->setText("Voltage: 0");
horizontalLabel2->setFont(QFont(font().family(), 9));
horizontalLabel2->setPen(QPen(Qt::green));
horizontalLabel2->setBrush(QBrush(Qt::white));
horizontalLabel2->setVisible(false);
// 创建差值标签
diffLabel = new QCPItemText(ui->customPlot);
diffLabel->setLayer("overlay");
diffLabel->setClipToAxisRect(false);
diffLabel->setPositionAlignment(Qt::AlignTop|Qt::AlignHCenter);
diffLabel->position->setType(QCPItemPosition::ptAxisRectRatio);
diffLabel->position->setCoords(0.5, 0.05); // 顶部中间位置
diffLabel->setFont(QFont(font().family(), 10, QFont::Bold));
diffLabel->setPen(QPen(Qt::black));
diffLabel->setBrush(QBrush(Qt::white));
diffLabel->setVisible(false);
// 初始化状态
isVerticalDragging1 = false;
isHorizontalDragging1 = false;
isVerticalDragging2 = false;
isHorizontalDragging2 = false;
cursorEditMode = false;
verticalLine1Created = false;
horizontalLine1Created = false;
verticalLine2Created = false;
horizontalLine2Created = false;
// 设置光标模式动作
cursorModeAction = new QAction("光标模式", this);
cursorModeAction->setIcon(QIcon(":/images/line_icon/rewind-button.png"));
cursorModeAction->setCheckable(true);
mainToolBar->addAction(cursorModeAction);
connect(cursorModeAction, &QAction::triggered, this, &MainWindow::on_cursorModeAction_triggered);
ui->statusbar->showMessage("光标编辑模式已关闭");
// 连接鼠标移动事件到 mouseMoveEvent 槽函数
connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMoveEvent(QMouseEvent*)));
connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePressEvent(QMouseEvent*)));
connect(ui->customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(mouseReleaseEvent(QMouseEvent*)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_cursorModeAction_triggered()
{
cursorEditMode = cursorModeAction->isChecked();
if (cursorEditMode) {
ui->customPlot->setInteractions(QCP::iSelectPlottables);
ui->statusbar->showMessage("光标编辑模式已开启 - 左键添加/移动垂直光标,右键添加/移动水平光标");
} else {
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
ui->statusbar->showMessage("光标编辑模式已关闭");
verticalLine1->setVisible(false);
horizontalLine1->setVisible(false);
verticalLabel1->setVisible(false);
horizontalLabel1->setVisible(false);
verticalLine2->setVisible(false);
horizontalLine2->setVisible(false);
verticalLabel2->setVisible(false);
horizontalLabel2->setVisible(false);
diffLabel->setVisible(false);
verticalLine1Created = false;
horizontalLine1Created = false;
verticalLine2Created = false;
horizontalLine2Created = false;
activeVerticalLine = nullptr;
activeVerticalLabel = nullptr;
activeHorizontalLine = nullptr;
activeHorizontalLabel = nullptr;
}
ui->customPlot->replot();
}
// void MainWindow::mousePressEvent(QMouseEvent *event)
// {
// if (cursorEditMode) {
// double x = ui->customPlot->xAxis->pixelToCoord(event->x());
// double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// // 检查点击是否靠近第一组垂直光标
// if (qAbs(x - verticalLine1->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
// isVerticalDragging1 = true;
// verticalLine1->setVisible(true);
// verticalLabel1->setVisible(true);
// verticalLine1Created = true;
// }
// // 检查点击是否靠近第一组水平光标
// if (qAbs(y - horizontalLine1->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
// isHorizontalDragging1 = true;
// horizontalLine1->setVisible(true);
// horizontalLabel1->setVisible(true);
// horizontalLine1Created = true;
// }
// // 检查点击是否靠近第二组垂直光标
// if (qAbs(x - verticalLine2->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
// isVerticalDragging2 = true;
// verticalLine2->setVisible(true);
// verticalLabel2->setVisible(true);
// verticalLine2Created = true;
// }
// // 检查点击是否靠近第二组水平光标
// if (qAbs(y - horizontalLine2->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
// isHorizontalDragging2 = true;
// horizontalLine2->setVisible(true);
// horizontalLabel2->setVisible(true);
// horizontalLine2Created = true;
// }
// // 如果点击在空白处,根据鼠标位置设置新光标位置
// if (event->button() == Qt::LeftButton) {
// if (!verticalLine1Created) {
// verticalLine1->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine1->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// verticalLine1->setVisible(true);
// verticalLabel1->setVisible(true);
// isVerticalDragging1 = true;
// verticalLine1Created = true;
// } else if (!verticalLine2Created) {
// verticalLine2->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine2->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// verticalLine2->setVisible(true);
// verticalLabel2->setVisible(true);
// isVerticalDragging2 = true;
// verticalLine2Created = true;
// }
// } else if (event->button() == Qt::RightButton) {
// if (!horizontalLine1Created) {
// horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// horizontalLine1->setVisible(true);
// horizontalLabel1->setVisible(true);
// isHorizontalDragging1 = true;
// horizontalLine1Created = true;
// } else if (!horizontalLine2Created) {
// horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// horizontalLine2->setVisible(true);
// horizontalLabel2->setVisible(true);
// isHorizontalDragging2 = true;
// horizontalLine2Created = true;
// }
// }
// ui->customPlot->replot();
// } else {
// QMainWindow::mousePressEvent(event);
// }
// }
// void MainWindow::mousePressEvent(QMouseEvent *event)
// {
// if (cursorEditMode) {
// double x = ui->customPlot->xAxis->pixelToCoord(event->x());
// double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// // 检查点击是否靠近垂直光标
// if (qAbs(x - verticalLine1->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
// activeVerticalLine = verticalLine1;
// activeVerticalLabel = verticalLabel1;
// isVerticalDragging1 = true;
// verticalLine1->setVisible(true);
// verticalLabel1->setVisible(true);
// verticalLine1Created = true;
// } else if (qAbs(x - verticalLine2->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
// activeVerticalLine = verticalLine2;
// activeVerticalLabel = verticalLabel2;
// isVerticalDragging2 = true;
// verticalLine2->setVisible(true);
// verticalLabel2->setVisible(true);
// verticalLine2Created = true;
// }
// // 检查点击是否靠近水平光标
// if (qAbs(y - horizontalLine1->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
// activeHorizontalLine = horizontalLine1;
// activeHorizontalLabel = horizontalLabel1;
// isHorizontalDragging1 = true;
// horizontalLine1->setVisible(true);
// horizontalLabel1->setVisible(true);
// horizontalLine1Created = true;
// } else if (qAbs(y - horizontalLine2->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
// activeHorizontalLine = horizontalLine2;
// activeHorizontalLabel = horizontalLabel2;
// isHorizontalDragging2 = true;
// horizontalLine2->setVisible(true);
// horizontalLabel2->setVisible(true);
// horizontalLine2Created = true;
// }
// // 如果点击在空白处,根据鼠标位置设置新光标位置
// if (event->button() == Qt::LeftButton) {
// if (!verticalLine1Created) {
// activeVerticalLine = verticalLine1;
// activeVerticalLabel = verticalLabel1;
// verticalLine1->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine1->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// verticalLine1->setVisible(true);
// verticalLabel1->setVisible(true);
// isVerticalDragging1 = true;
// verticalLine1Created = true;
// } else if (!verticalLine2Created) {
// activeVerticalLine = verticalLine2;
// activeVerticalLabel = verticalLabel2;
// verticalLine2->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine2->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// verticalLine2->setVisible(true);
// verticalLabel2->setVisible(true);
// isVerticalDragging2 = true;
// verticalLine2Created = true;
// }
// } else if (event->button() == Qt::RightButton) {
// if (!horizontalLine1Created) {
// activeHorizontalLine = horizontalLine1;
// activeHorizontalLabel = horizontalLabel1;
// horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// horizontalLine1->setVisible(true);
// horizontalLabel1->setVisible(true);
// isHorizontalDragging1 = true;
// horizontalLine1Created = true;
// } else if (!horizontalLine2Created) {
// activeHorizontalLine = horizontalLine2;
// activeHorizontalLabel = horizontalLabel2;
// horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// horizontalLine2->setVisible(true);
// horizontalLabel2->setVisible(true);
// isHorizontalDragging2 = true;
// horizontalLine2Created = true;
// }
// }
// ui->customPlot->replot();
// } else {
// QMainWindow::mousePressEvent(event);
// }
// }
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (cursorEditMode) {
double x = ui->customPlot->xAxis->pixelToCoord(event->x());
double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// 重置拖动状态
isVerticalDragging1 = false;
isHorizontalDragging1 = false;
isVerticalDragging2 = false;
isHorizontalDragging2 = false;
// 检查垂直光标(仅左键处理)
if (event->button() == Qt::LeftButton) {
if (verticalLine1Created && qAbs(x - verticalLine1->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
isVerticalDragging1 = true;
} else if (verticalLine2Created && qAbs(x - verticalLine2->point1->coords().x()) < CURSOR_DISTANCE_THRESHOLD) {
isVerticalDragging2 = true;
}
}
// 检查水平光标(仅右键处理)
if (event->button() == Qt::RightButton) {
if (horizontalLine1Created && qAbs(y - horizontalLine1->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
isHorizontalDragging1 = true;
} else if (horizontalLine2Created && qAbs(y - horizontalLine2->point1->coords().y()) < CURSOR_DISTANCE_THRESHOLD) {
isHorizontalDragging2 = true;
}
}
// 如果点击在空白处,根据鼠标位置设置新光标位置
if (event->button() == Qt::LeftButton) {
if (!verticalLine1Created) {
verticalLine1->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
verticalLine1->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
verticalLine1->setVisible(true);
verticalLabel1->setVisible(true);
isVerticalDragging1 = true;
verticalLine1Created = true;
// 更新标签文本
//verticalLabel1->setText(QString("Time: %1").arg(x, 0, 'f', 2));
updateVerticalCursorLabel(verticalLabel1, x);
} else if (!verticalLine2Created) {
verticalLine2->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
verticalLine2->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
verticalLine2->setVisible(true);
verticalLabel2->setVisible(true);
isVerticalDragging2 = true;
verticalLine2Created = true;
// 更新标签文本
//verticalLabel1->setText(QString("Time: %1").arg(x, 0, 'f', 2));
updateVerticalCursorLabel(verticalLabel2, x);
}
} else if (event->button() == Qt::RightButton) {
if (!horizontalLine1Created) {
horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
horizontalLine1->setVisible(true);
horizontalLabel1->setVisible(true);
isHorizontalDragging1 = true;
horizontalLine1Created = true;
// 更新标签文本
//horizontalLabel1->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
updateHorizontalCursorLabel(horizontalLabel1, y);
} else if (!horizontalLine2Created) {
horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
horizontalLine2->setVisible(true);
horizontalLabel2->setVisible(true);
isHorizontalDragging2 = true;
horizontalLine2Created = true;
// 更新标签文本
//horizontalLabel1->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
updateHorizontalCursorLabel(horizontalLabel2, y);
}
}
ui->customPlot->replot();
} else {
QMainWindow::mousePressEvent(event);
}
}
// void MainWindow::mouseMoveEvent(QMouseEvent *event)
// {
// if (cursorEditMode) {
// double x = ui->customPlot->xAxis->pixelToCoord(event->x());
// double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// if (isVerticalDragging1) {
// verticalLine1->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine1->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// // 更新垂直标签位置和内容
// verticalLabel1->position->setCoords(
// (x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(),
// 1.0
// );
// verticalLabel1->setText(QString("Time: %1").arg(x, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Time: %1").arg(x, 0, 'f', 2));
// }
// if (isHorizontalDragging1) {
// horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// // 更新水平标签位置和内容
// horizontalLabel1->position->setCoords(
// 0.0,
// (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size()
// );
// horizontalLabel1->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Voltage: %1").arg(y, 0, 'f', 2));
// }
// if (isVerticalDragging2) {
// verticalLine2->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine2->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// // 更新垂直标签位置和内容
// verticalLabel2->position->setCoords(
// (x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(),
// 1.0
// );
// verticalLabel2->setText(QString("Time: %1").arg(x, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Time: %1").arg(x, 0, 'f', 2));
// }
// if (isHorizontalDragging2) {
// horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// // 更新水平标签位置和内容
// horizontalLabel2->position->setCoords(
// 0.0,
// (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size()
// );
// horizontalLabel2->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Voltage: %1").arg(y, 0, 'f', 2));
// }
// ui->customPlot->replot();
// calculateAndDisplayDifferences();
// } else {
// QMainWindow::mouseMoveEvent(event);
// }
// }
// void MainWindow::mouseMoveEvent(QMouseEvent *event)
// {
// if (cursorEditMode) {
// double x = ui->customPlot->xAxis->pixelToCoord(event->x());
// double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// if (isVerticalDragging1 || isVerticalDragging2) {
// activeVerticalLine->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// activeVerticalLine->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
// // 更新垂直标签位置和内容
// activeVerticalLabel->position->setCoords(
// (x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(),
// 1.0
// );
// activeVerticalLabel->setText(QString("Time: %1").arg(x, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Time: %1").arg(x, 0, 'f', 2));
// }
// if (isHorizontalDragging1 || isHorizontalDragging2) {
// activeHorizontalLine->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// activeHorizontalLine->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
// // 更新水平标签位置和内容
// activeHorizontalLabel->position->setCoords(
// 0.0,
// (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size()
// );
// activeHorizontalLabel->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
// ui->statusbar->showMessage(QString("Voltage: %1").arg(y, 0, 'f', 2));
// }
// ui->customPlot->replot();
// calculateAndDisplayDifferences();
// } else {
// QMainWindow::mouseMoveEvent(event);
// }
// }
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if (cursorEditMode) {
double x = ui->customPlot->xAxis->pixelToCoord(event->x());
double y = ui->customPlot->yAxis->pixelToCoord(event->y());
// 独立处理垂直光标拖动
if (isVerticalDragging1) {
// verticalLine1->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine1->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
verticalLine1->point1->setCoords(x, verticalLine1->point1->coords().y());
verticalLine1->point2->setCoords(x, verticalLine1->point2->coords().y());
verticalLabel1->position->setCoords(
(x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(), 1.0);
verticalLabel1->setText(QString("Time: %1").arg(x, 0, 'f', 2));
} else if (isVerticalDragging2) {
// verticalLine2->point1->setCoords(x, ui->customPlot->yAxis->range().lower);
// verticalLine2->point2->setCoords(x, ui->customPlot->yAxis->range().upper);
verticalLine2->point1->setCoords(x, verticalLine2->point1->coords().y());
verticalLine2->point2->setCoords(x, verticalLine2->point2->coords().y());
verticalLabel2->position->setCoords(
(x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(), 1.0);
verticalLabel2->setText(QString("Time: %1").arg(x, 0, 'f', 2));
}
// 独立处理水平光标拖动
if (isHorizontalDragging1) {
// horizontalLine1->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine1->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
horizontalLine1->point1->setCoords(horizontalLine1->point1->coords().x(), y);
horizontalLine1->point2->setCoords(horizontalLine2->point2->coords().x(), y);
horizontalLabel1->position->setCoords(
0.0, (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size());
horizontalLabel1->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
} else if (isHorizontalDragging2) {
// horizontalLine2->point1->setCoords(ui->customPlot->xAxis->range().lower, y);
// horizontalLine2->point2->setCoords(ui->customPlot->xAxis->range().upper, y);
horizontalLine2->point1->setCoords(horizontalLine2->point1->coords().x(), y);
horizontalLine2->point2->setCoords(horizontalLine2->point2->coords().x(), y);
horizontalLabel2->position->setCoords(
0.0, (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size());
horizontalLabel2->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
}
ui->customPlot->replot();
calculateAndDisplayDifferences();
qDebug() << "Creating horizontal line 2 at y:" << y;
} else {
QMainWindow::mouseMoveEvent(event);
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
isVerticalDragging1 = false;
isHorizontalDragging1 = false;
isVerticalDragging2 = false;
isHorizontalDragging2 = false;
if (!cursorEditMode) {
QMainWindow::mouseReleaseEvent(event);
}
}
void MainWindow::calculateAndDisplayDifferences()
{
static double lastTimeDiff = 0;
static double lastVoltageDiff = 0;
QString statusMessage;
bool needsUpdate = false;
// 计算并显示时间差值
if (verticalLine1->visible() && verticalLine2->visible()) {
double timeDiff = qAbs(verticalLine1->point1->coords().x() - verticalLine2->point1->coords().x());
if (!qFuzzyCompare(timeDiff, lastTimeDiff)) {
statusMessage += QString("ΔT: %1 ").arg(timeDiff, 0, 'f', 2);
lastTimeDiff = timeDiff;
needsUpdate = true;
}
}
// 计算并显示电压差值
if (horizontalLine1->visible() && horizontalLine2->visible()) {
double voltageDiff = qAbs(horizontalLine1->point1->coords().y() - horizontalLine2->point1->coords().y());
if (!qFuzzyCompare(voltageDiff, lastVoltageDiff)) {
statusMessage += QString("ΔV: %1").arg(voltageDiff, 0, 'f', 2);
lastVoltageDiff = voltageDiff;
needsUpdate = true;
}
}
// 仅在需要时更新UI
if (needsUpdate) {
ui->statusbar->showMessage(statusMessage);
if (diffLabel) {
diffLabel->setText(statusMessage);
diffLabel->setVisible(!statusMessage.isEmpty());
}
ui->customPlot->replot(QCustomPlot::rpQueuedReplot);
}
}
void MainWindow::updateVerticalCursorLabel(QCPItemText *label, double x)
{
label->position->setCoords(
(x - ui->customPlot->xAxis->range().lower) / ui->customPlot->xAxis->range().size(), 1.0);
label->setText(QString("Time: %1").arg(x, 0, 'f', 2));
}
void MainWindow::updateHorizontalCursorLabel(QCPItemText *label, double y)
{
label->position->setCoords(
0.0, (y - ui->customPlot->yAxis->range().lower) / ui->customPlot->yAxis->range().size());
label->setText(QString("Voltage: %1").arg(y, 0, 'f', 2));
qDebug() << "Setting horizontal label 2 text to voltage:" << y;
}
在QCustomPlot中实现双光标测量两点之间的差值
最新发布