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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM