VerticalLabel的简单实现

本文介绍了一种在Windows Forms应用程序中实现垂直显示文本标签的方法。通过自定义Label控件,文章详细展示了如何调整文本方向并控制其布局。此外,还提供了一个示例项目,展示如何使用属性网格来调整垂直标签的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一早收到CodeProject的订阅邮件,大概看了一下。里面有一个做VerticalLabel的例子,我没看,就想自己做一个试试。抛砖引玉!

VerticalLabel.JPG

None.gifusing System;
None.gif
using System.Drawing;
None.gif
using System.Windows.Forms;
None.gif
None.gif
namespace Webb.Publics.WinForm
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for VerticalLabel.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class VerticalLabel : System.Windows.Forms.Label
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int _Span = 0;
InBlock.gif        
public int TextSpan
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return this._Span;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{
InBlock.gif                
this._Span = value;
InBlock.gif                
this.Refresh();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public VerticalLabel()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: Add constructor logic here
InBlock.gif            
//    
InBlock.gif
            this.Width = 35;
InBlock.gif            
this.Height = 100;
ExpandedSubBlockEnd.gif        }
    
InBlock.gif
InBlock.gif        
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//base.OnPaint (e);
InBlock.gif
            SizeF m_size = e.Graphics.MeasureString(this.Text,this.Font);
InBlock.gif            
float m_SingleWidth = m_size.Width/this.Text.Length+2;
InBlock.gif            
float m_SingleHeight = m_size.Height;
InBlock.gif            
float m_TotalWidth = m_SingleWidth;
InBlock.gif            
float m_TotalHeight = m_size.Height*this.Text.Length;
InBlock.gif            
//int i = 0;
InBlock.gif
            PointF m_Offset = this.MeasurePosition(m_TotalWidth,m_TotalHeight);
InBlock.gif            
for(int i = 0;i<this.Text.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                RectangleF m_Rec 
= new RectangleF(m_Offset.X,m_Offset.Y+(m_SingleHeight+this.TextSpan)*i,m_SingleWidth,m_SingleHeight);
InBlock.gif                e.Graphics.DrawString(
this.Text.Substring(i,1),this.Font,new SolidBrush(this.ForeColor),m_Rec);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private PointF MeasurePosition(float i_TotalWidth,float i_TotalHeight)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PointF m_Result;
InBlock.gif            
switch(this.TextAlign)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//Bottom
InBlock.gif
                case ContentAlignment.BottomCenter:
InBlock.gif                    m_Result 
= new PointF((this.Width-i_TotalWidth)/2,this.Height-i_TotalHeight);
InBlock.gif                    
break;
InBlock.gif                
case ContentAlignment.BottomLeft:
InBlock.gif                    m_Result 
= new PointF(0,this.Height-i_TotalHeight);
InBlock.gif                    
break;
InBlock.gif                
case ContentAlignment.BottomRight:
InBlock.gif                    m_Result 
= new PointF(this.Width-i_TotalWidth,this.Height-i_TotalHeight);
InBlock.gif                    
break;
InBlock.gif                
//Middle
InBlock.gif
                case ContentAlignment.MiddleCenter:
InBlock.gif                    m_Result 
= new PointF((this.Width-i_TotalWidth)/2,(this.Height-i_TotalHeight)/2);
InBlock.gif                    
break;
InBlock.gif                
case ContentAlignment.MiddleLeft:
InBlock.gif                    m_Result 
= new PointF(0,(this.Height-i_TotalHeight)/2);
InBlock.gif                    
break;
InBlock.gif                
case ContentAlignment.MiddleRight:
InBlock.gif                    m_Result 
= new PointF(this.Width-i_TotalWidth,(this.Height-i_TotalHeight)/2);
InBlock.gif                    
break;
InBlock.gif                
//Top
InBlock.gif
                case ContentAlignment.TopCenter:
InBlock.gif                    m_Result 
= new PointF((this.Width-i_TotalWidth)/2,0);
InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                
case ContentAlignment.TopLeft:
InBlock.gif                    m_Result 
= new PointF(0,0);
InBlock.gif                    
break;                
InBlock.gif                
case ContentAlignment.TopRight:
InBlock.gif                    m_Result 
= new PointF(this.Width-i_TotalWidth,0);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return m_Result;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

None.gifusing System;
None.gif
using System.Drawing;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Windows.Forms;
None.gif
None.gif
namespace WindowsApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Form5.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form5 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.PropertyGrid propertyGrid1;
InBlock.gif        
private Webb.Publics.WinForm.VerticalLabel verticalLabel2;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required designer variable.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form5()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Required for Windows Form Designer support
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: Add any constructor code after InitializeComponent call
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Clean up any resources being used.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows Form Designer generated code#region Windows Form Designer generated code
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
InBlock.gif            
this.verticalLabel2 = new Webb.Publics.WinForm.VerticalLabel();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// propertyGrid1
InBlock.gif            
// 
InBlock.gif
            this.propertyGrid1.CommandsVisibleIfAvailable = true;
InBlock.gif            
this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Right;
InBlock.gif            
this.propertyGrid1.LargeButtons = false;
InBlock.gif            
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
InBlock.gif            
this.propertyGrid1.Location = new System.Drawing.Point(3040);
InBlock.gif            
this.propertyGrid1.Name = "propertyGrid1";
InBlock.gif            
this.propertyGrid1.PropertySort = System.Windows.Forms.PropertySort.Alphabetical;
InBlock.gif            
this.propertyGrid1.SelectedObject = this.verticalLabel2;
InBlock.gif            
this.propertyGrid1.Size = new System.Drawing.Size(272269);
InBlock.gif            
this.propertyGrid1.TabIndex = 1;
InBlock.gif            
this.propertyGrid1.Text = "propertyGrid1";
InBlock.gif            
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
InBlock.gif            
this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
InBlock.gif            
// 
InBlock.gif            
// verticalLabel2
InBlock.gif            
// 
InBlock.gif
            this.verticalLabel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
InBlock.gif            
this.verticalLabel2.Location = new System.Drawing.Point(88);
InBlock.gif            
this.verticalLabel2.Name = "verticalLabel2";
InBlock.gif            
this.verticalLabel2.Size = new System.Drawing.Size(288256);
InBlock.gif            
this.verticalLabel2.TabIndex = 3;
InBlock.gif            
this.verticalLabel2.Text = "这是一个测试";
InBlock.gif            
this.verticalLabel2.TextSpan = 0;
InBlock.gif            
// 
InBlock.gif            
// Form5
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(576269);
InBlock.gif            
this.Controls.Add(this.verticalLabel2);
InBlock.gif            
this.Controls.Add(this.propertyGrid1);
InBlock.gif            
this.Name = "Form5";
InBlock.gif            
this.Text = "Form5";
InBlock.gif            
this.Load += new System.EventHandler(this.Form5_Load);
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void Form5_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
#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中实现双光标测量两点之间的差值
07-10
void MainWindow::onMeasurementModeChanged(int index) { qDebug() << "onMeasurementModeChanged called with index:" << index; // 根据选择的测量模式进行相应的处理 if (ui->measurementModeComboBox->currentText() == "电压测量") { qDebug() << "Switching to Voltage Measurement mode"; currentMeasurementMode = VoltageMeasurement; ui->CursorModelabel->setText("电压测量模式"); // 隐藏垂直光标和标签 verticalLine1->setVisible(false); verticalLine2->setVisible(false); verticalLabel1->setVisible(false); verticalLabel2->setVisible(false); // 显示水平光标和标签 horizontalLine1->setVisible(true); horizontalLine2->setVisible(true); horizontalLabel1->setVisible(true); horizontalLabel2->setVisible(true); // 重置状态 verticalLine1Created = false; verticalLine2Created = false; horizontalLine1Created = false; horizontalLine2Created = false; } else if (ui->measurementModeComboBox->currentText() == "时间测量") { qDebug() << "Switching to Time Measurement mode"; currentMeasurementMode = TimeMeasurement; ui->CursorModelabel->setText("时间测量模式"); // 隐藏水平光标和标签 horizontalLine1->setVisible(false); horizontalLine2->setVisible(false); horizontalLabel1->setVisible(false); horizontalLabel2->setVisible(false); // 显示垂直光标和标签 verticalLine1->setVisible(true); verticalLine2->setVisible(true); verticalLabel1->setVisible(true); verticalLabel2->setVisible(true); // 重置状态 verticalLine1Created = false; verticalLine2Created = false; horizontalLine1Created = false; horizontalLine2Created = false; // 确保垂直光标有正确的初始位置 double xCenter = (ui->customplot->xAxis->range().lower + ui->customplot->xAxis->range().upper) / 2; setupVerticalCursor(verticalLine1, verticalLabel1, xCenter - 10); setupVerticalCursor(verticalLine2, verticalLabel2, xCenter + 10); } ui->customplot->replot(); }//测量模式下拉列表选项改变信号槽连接 connect(ui->measurementModeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::onMeasurementModeChanged);QCustomPlot在测量模式下切换QComboBox时,水平光标或垂直光标不显示的问题。 根据用户描述,问题出现在使用QComboBox切换测量模式时,对应的水平或垂直光标没有显示出来
07-12
内容概要:该论文聚焦于T2WI核磁共振图像超分辨率问题,提出了一种利用T1WI模态作为辅助信息的跨模态解决方案。其主要贡献包括:提出基于高频信息约束的网络框架,通过主干特征提取分支和高频结构先验建模分支结合Transformer模块和注意力机制有效重建高频细节;设计渐进式特征匹配融合框架,采用多阶段相似特征匹配算法提高匹配鲁棒性;引入模型量化技术降低推理资源需求。实验结果表明,该方法不仅提高了超分辨率性能,还保持了图像质量。 适合人群:从事医学图像处理、计算机视觉领域的研究人员和工程师,尤其是对核磁共振图像超分辨率感兴趣的学者和技术开发者。 使用场景及目标:①适用于需要提升T2WI核磁共振图像分辨率的应用场景;②目标是通过跨模态信息融合提高图像质量,解决传统单模态方法难以克服的高频细节丢失问题;③为临床诊断提供更高质量的影像资料,帮助医生更准确地识别病灶。 其他说明:论文不仅提供了详细的网络架构设计与实现代码,还深入探讨了跨模态噪声的本质、高频信息约束的实现方式以及渐进式特征匹配的具体过程。此外,作者还对模型进行了量化处理,使得该方法可以在资源受限环境下高效运行。阅读时应重点关注论文中提到的技术创新点及其背后的原理,理解如何通过跨模态信息融合提升图像重建效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值