1. 程式人生 > >QCustomPlot 鼠標消息獲取,以及對應坐標轉換

QCustomPlot 鼠標消息獲取,以及對應坐標轉換

信號 () .cpp 消息 signal 鼠標移動 tom plot bsp

1 首先在 MainWindow.h 中加入 消息處理程序(槽)

private slots:
  void my_mouseMove(QMouseEvent* event);

2 在 MainWindow.cpp 中實現 (槽)

void MainWindow::my_mouseMove(QMouseEvent* event)
{
 //獲取鼠標坐標點
    int x_pos = event->pos().x(); 
    int y_pos = event->pos().y();

// 把鼠標坐標點 轉換為 QCustomPlot 內部坐標值 (pixelToCoord 函數)
// coordToPixel 函數與之相反 是把內部坐標值 轉換為外部坐標點 float x_val = ui->plot->xAxis->pixelToCoord(x_pos); float y_val = ui->plot->yAxis->pixelToCoord(y_pos); // 然後打印在界面上 ui->label->setText(tr("(%1 %2 || %3 %4)").arg(x_pos).arg(y_pos).arg(x_val).arg(y_val)); }

3 把 QCustomPlot

的 鼠標移動信號 與 槽 鏈接 起來

connect(ui->plot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(my_mouseMove(QMouseEvent*)));

QCustomPlot 鼠標消息獲取,以及對應坐標轉換