1. 程式人生 > >Qt QCustomPlot 取資料,滑鼠移動顯示

Qt QCustomPlot 取資料,滑鼠移動顯示

取資料:

for (int i = 0;i < ui->plot->graph(0)->dataCount();i++) {
      float x = ui->plot->graph(0)->data()->at(i)->key;
      float y = ui->plot->graph(0)->data()->at(i)->value;
      qDebug() << x << y << endl;
}

滑鼠移動:

connect(ui->plot, SIGNAL(mouseMove(QMouseEvent*)), this
, SLOT(myMoveEvent(QMouseEvent*))); void MainWindow::myMoveEvent(QMouseEvent *event) { //獲取滑鼠座標,相對父窗體座標 int x_pos = event->pos().x(); int y_pos = event->pos().y(); //滑鼠座標轉化為CustomPlot內部座標 float x_val = ui->plot->xAxis->pixelToCoord(x_pos); float y_val = ui->plot->yAxis->pixelToCoord(y_pos); QString str,strToolTip; str
= QString::number(x_val, 10, 3); strToolTip += "x: "; strToolTip += str; strToolTip += "\n"; for (int i = 0;i < ui->plot->xAxis->graphs().count();i++) { //獲得x軸座標位置對應的曲線上y的值 float y = ui->plot->graph(i)->data()->at(x_val)->value; str
= QString::number(y); strToolTip += "y" + QString::number(i) + ":"; strToolTip += str; strToolTip += "\n"; } QToolTip::showText(cursor().pos(), strToolTip, ui->plot); }