2013-08-09 19 views
9

私はlsLineのラインスタイルを持っている私のQCustomPlotに異なるポイントのプロット値を表示しようとしています。私はQCustomPlotで信号上にマウスを置くことができることを知っているが、それはマウスが私のプロットされたline.Myの上にあるときに通知する必要があるので、本当に助けてあげないだろう私の質問は、ポイント。マウスが散乱点の上に来たときに私に教えてくれる信号がありますか?マウスの上にプロット値を表示します。 - スキャッタポイントを検出する

答えて

9

QCustomPlot::mouseMoveEventを再実装するか、QCustomPlot::mouseMoveに接続してください。

次に、軸のcoordToPixelを使用してピクセル座標をプロット座標に変換し、QCPDataMapの最も近い点をQMap::lowerBound(cursorX)で検索します。

+0

(接続された信号QCustomplot::MouseMoveで) 'coordToPixel'はピクセル座標にプロット座標変換する最速の方法

です。どのようにあなたの問題を解決しましたか? – Nejat

6

QCustomPlotが発信するmouseMove信号にスロットを接続するだけで簡単に接続できます。その後、座標見つけるためQCPAxis::pixelToCoordを使用することができます。あなたは、X軸(第2あたりの点を含む)日時フォーマットを使用するとき

+0

ui、 'ui-> widget_graph1'と' ui-> widget_graph2'に2つのグラフがある場合、どのように両方のグラフに対してそれを行うことができますか?私のケースに合わせて 'void CustomPlot :: showPointToolTip(QMouseEvent * event){}'という関数名を変更する必要がありますか?私は両方のグラフにマウスのホバー上の座標を表示したい、 – Wei

+0

@Wei私が行ったように 'QCustomPlot'ソースコードでスロットを実装すると、すべてのプロットのツールチップが表示されます。別のクラスにスロットを置いて、 'sender()'を使って 'mouseMove'シグナルを送出したプロットを見つけることもできます。 – Nejat

+0

私は 'QCustomPlot :: toolTip'しか見つけず、あなたの' void QCustomPlot :: showPointToolTip(QMouseEvent * event){} 'を' void QCustomPlot :: toolTip(QMouseEvent * event){} 'に変更しました。 ? – Wei

2
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)); 

} 

、次いで、画素が失敗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; 
     } 
} 
関連する問題