0
我想在鼠標移動圖上顯示x和y座標。 我計算出的X和Y座標,但真的不知道如何在圖表上顯示附近的點(最好是像(x,y)
這在QCustomplot圖上顯示x和y座標
connect(ui->customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(mouseRelease(QMouseEvent*)));
connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress(QMouseEvent*)));
float MainWindow::findX(QCustomPlot* customPlot, QCPCursor* cursor1, QMouseEvent* event)
{
double x = customPlot->xAxis->pixelToCoord(event->pos().x());
double y = customPlot->yAxis->pixelToCoord(event->pos().y());
// I think I need to write a function here which will display the text on the graph.
}
void MainWindow::mouseRelease(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
static QCPCursor cursor1;
QCustomPlot* plot = (QCustomPlot*)sender();
float x = findX(plot, &cursor1, event);
}
}
void MainWindow::mousePressed(QMouseEvent* event)
{
if (event->button() == Qt::RightButton) {
QCustomPlot* plot = (QCustomPlot*)sender();
plot->setContextMenuPolicy(Qt::CustomContextMenu);
connect(plot, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(ShowContextMenu(const QPoint&)));
}
}