最近在用qcustomplot做一个项目,记录一下,也是怕自己忘记
图一:实现的效果图。
使用鼠标移动触发。(部分项目代码,自己在家没事的时候,折腾出来的,不涉及公司的代码)
实现的功能 鼠标右键后 释放(线段不随随便的移动而移动)
if(ui->customPlot->graph(1)->selected() && mflag.axisMflag_x1==2)
{
mflag.axisMflag_x1 = 3; //=3的时候是不随游标进行移动
}
else if(mflag.axisMmflag_x1 == 1 ) //进行了第一次
{
mflag.axisMflag_x2 = 3;
}
功能 鼠标移动后,游标随鼠标的移动而移动,不过前提是要先选中。
void MainWindow::showTracer(QMouseEvent *event)
{
double double_x = ui->customPlot->xAxis->pixelToCoord(event->pos().x()) ;
double double_y = ui->customPlot->yAxis->pixelToCoord(event->pos().y()) ;
cursor_x = double_x ;//获得当前鼠标的位置
cursor_y = double_y ;//获得当前鼠标的位置
showCursor() ;
}
void MainWindow::triggerCurrentLine(double y)
{
if( mflag.axisMflag_x1 == 1 )
{
y1_cursorData = cursor_y;
cursorX4[0] = ui -> customPlot->xAxis->range().upper ;
cursorX4[1] = ui -> customPlot->xAxis->range().lower ;
cursorY4[0] = y1_cursorData ;
cursorY4[1] = y1_cursorData ;
ui->customPlot->graph(1)->setData( cursorX4, cursorY4 );
ui->lineEdit_TrigerUnit->setText( QString::number( y1_cursorData ,'f', 1)) ;
}
else if( mflag.axisMflag_x1 == 3 ) // 查看鼠标右键
{
if( ui->customPlot->graph(1)->selected() )
{
y1_cursorData = cursor_y;
cursorX4[0] = ui -> customPlot->xAxis->range().upper ;
cursorX4[1] = ui -> customPlot->xAxis->range().lower ;
cursorY4[0] = y1_cursorData ;
cursorY4[1] = y1_cursorData ;
ui->customPlot->graph(1)->setData( cursorX4, cursorY4 );
qDebug("%f",y1_cursorData);
}
else
{
cursorX4[0] = ui -> customPlot->xAxis->range().upper ;
cursorX4[1] = ui -> customPlot->xAxis->range().lower ;
cursorY4[0] = y1_cursorData ;
cursorY4[1] = y1_cursorData ;
ui->customPlot->graph(1)->setData(cursorX4, cursorY4) ;
qDebug("%f",y1_cursorData);
}
ui->lineEdit_TrigerUnit->setText( QString::number( y1_cursorData ,'f', 1)) ;
}
else if (mflag.axisMflag_x3 == 0)
{
cursorX4[0] = 0 ;
cursorX4[1] = 0 ;
cursorY4[0] = 0 ;
cursorY4[1] = 0 ;
ui->customPlot->graph(1)->setData(cursorX4, cursorY4);
ui->lineEdit_TrigerUnit->setText( QString::number( 0,'f', 1)) ;
}
switch (ui->y_unit_comboBox->currentIndex())
{
case 0 : ui->comboBoxTrigerUnit->setCurrentIndex(0);break;
case 1: ui->comboBoxTrigerUnit->setCurrentIndex(1);break;
case 2: ui->comboBoxTrigerUnit->setCurrentIndex(2); break;
default: break;
}
}