2013-08-09 6 views
9

lsLine의 선 스타일이있는 QCustomPlot에 다른 점의 플롯 값을 표시하려고합니다. 나는 QCustomPlot에있는 신호 위에 마우스를 설정할 수 있다는 것을 알고 있지만 마우스가 내 플롯 된 선 위에있을 때 정보를 얻으려고하기 때문에 실제로 도움이되지는 않을 것입니다. 내 질문은 마우스가 내 분산 장치 위에 있는지 확인하는 방법입니다 포인트. 마우스가 흩어지는 지점에있을 때 알려주는 신호가 있습니까?마우스를 올리면 플롯 값이 표시됩니다. - 분산 점 감지

답변

9

QCustomPlot::mouseMoveEvent을 다시 구현하거나 QCustomPlot::mouseMove에 연결하십시오.

축의 coordToPixel을 사용하여 픽셀 좌표를 플롯 좌표로 변환하고 QCPDataMap의 가장 가까운 점을 QMap::lowerBound(cursorX)으로 검색하십시오.

+0

(연결 신호 QCustomplot::MouseMove으로)'coordToPixel' 픽셀 좌표에 그래프 좌표를 변환하는 가장 빠른 방법

입니다. 문제가 어떻게 해결 되었습니까? – Nejat

6

슬롯을 QCustomPlot이 방출하는 mouseMove 신호에 쉽게 연결할 수 있습니다. 그런 다음 좌표를 찾기 위해 QCPAxis::pixelToCoord을 사용할 수 있습니다 : 당신은 X 축 (초당 점 포함) 날짜 형식을 사용하는 경우

connect(this, SIGNAL(mouseMove(QMouseEvent*)), this,SLOT(showPointToolTip(QMouseEvent*))); 

void QCustomPlot::showPointToolTip(QMouseEvent *event) 
{ 

    int x = this->xAxis->pixelToCoord(event->pos().x()); 
    int y = this->yAxis->pixelToCoord(event->pos().y()); 

    setToolTip(QString("%1 , %2").arg(x).arg(y)); 

} 
+0

ui에 두 개의 그래프가있는 경우 'ui-> widget_graph1' 및'ui-> widget_graph2' 두 그래프에 대해 어떻게 할 수 있습니까? 내 케이스에 맞게 void CustomPlot :: showPointToolTip (QMouseEvent * event) {}'함수 이름을 변경해야합니까? 두 그래프 모두 마우스를 가져 가면 좌표가 표시됩니다. – Wei

+0

@Wei 'QCustomPlot' 소스 코드에서 슬롯을 구현하면 모든 팁에 대해 툴팁이 표시됩니다. 또 다른 클래스에 슬롯을 넣고'sender()'를 사용하여'mouseMove' 신호를 내린 플롯을 찾을 수 있습니다. – Nejat

+0

'QCustomPlot :: toolTip' 만 찾았고'void QCustomPlot :: showPointToolTip (QMouseEvent * 이벤트) {}'를 void QCustomPlot :: toolTip (QMouseEvent * event) {}'으로 변경했습니다. ? – Wei

2

, 다음 픽셀은 실패 COORD 할 수 있습니다. 이 점 사이의 좌표를 표시하려면 는 다음이 아마 유용이 @Rajeshwar

void MainWindow::onMouseMoveGraph(QMouseEvent* evt) 
    { 
    int x = this->ui->customPlot->xAxis->pixelToCoord(evt->pos().x()); 
    int y = this->ui->customPlot->yAxis->pixelToCoord(evt->pos().y()); 
    qDebug()<<"pixelToCoord: "<<data.key<<data.value; //this is correct when step is greater 1 second 

if (this->ui->customPlot->selectedGraphs().count()>0) 
     { 
     QCPGraph* graph = this->ui->customPlot->selectedGraphs().first(); 
     QCPData data = graph->data()->lowerBound(x).value(); 

     double dbottom = graph->valueAxis()->range().lower;  //Yaxis bottom value 
     double dtop = graph->valueAxis()->range().upper;   //Yaxis top value 
     long ptop = graph->valueAxis()->axisRect()->top();   //graph top margin 
     long pbottom = graph->valueAxis()->axisRect()->bottom(); //graph bottom position 
// result for Y axis 
     double valueY = (evt->pos().y() - ptop)/(double)(pbottom - ptop)*(double)(dbottom - dtop) + dtop; 

//or shortly for X-axis 
     double valueX = (evt->pos().x() - graph->keyAxis()->axisRect()->left()); //graph width in pixels 
     double ratio = (double)(graph->keyAxis()->axisRect()->right() - graph->keyAxis()->axisRect()->left())/(double)(graph->keyAxis()->range().lower - graph->keyAxis()->range().upper); //ratio px->graph width 
//and result for X-axis 
     valueX=-valueX/ratio + graph->keyAxis()->range().lower; 


     qDebug()<<"calculated:"<<valueX<<valueY; 
     } 
}