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


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

 


免責聲明!

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



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