10

我试图在QCustomPlot我的线条样式中显示不同点的绘图值lsLine。我知道我可以在上面设置鼠标悬停信号,QCustomPlot但这并没有真正的帮助,因为我只需要在鼠标悬停在我的绘制线上时得到通知。我的问题是有什么方法可以确定鼠标是否悬停在我的散点上. 是否有我可以连接的信号告诉我鼠标何时位于散点上方?

4

3 回答 3

10

您可以轻松地将插槽连接到发出的mouseMove信号QCustomPlot。然后,您可以使用QCPAxis::pixelToCoord来查找坐标:

connect(this, SIGNAL(mouseMove(QMouseEvent*)), this,SLOT(showPointToolTip(QMouseEvent*)));

void QCustomPlot::showPointToolTip(QMouseEvent *event)
{

    int x = this->xAxis->pixelToCoord(event->pos().x());
    int y = this->yAxis->pixelToCoord(event->pos().y());

    setToolTip(QString("%1 , %2").arg(x).arg(y));

}
于 2014-05-14T12:48:17.463 回答
9

重新实现QCustomPlot::mouseMoveEvent或连接到QCustomPlot::mouseMove.

然后使用 axes'coordToPixel平移(光标)像素坐标以绘制坐标并在您的QCPDataMapwith中搜索最近的点QMap::lowerBound(cursorX)

于 2013-11-10T18:48:06.377 回答
2

当您使用 X 轴的日期时间格式(包括每秒更多点)时,像素到坐标失败。如果要显示点之间的坐标,那么这是最快的方法

可能有用(有连接信号QCustomplot::MouseMove

void MainWindow::onMouseMoveGraph(QMouseEvent* evt)
    {
    int x = this->ui->customPlot->xAxis->pixelToCoord(evt->pos().x());
    int y = this->ui->customPlot->yAxis->pixelToCoord(evt->pos().y());
    qDebug()<<"pixelToCoord: "<<data.key<<data.value; //this is correct when step is greater 1 second

if (this->ui->customPlot->selectedGraphs().count()>0)
        {
        QCPGraph* graph = this->ui->customPlot->selectedGraphs().first();
        QCPData data = graph->data()->lowerBound(x).value();

        double dbottom = graph->valueAxis()->range().lower;        //Yaxis bottom value
        double dtop = graph->valueAxis()->range().upper;           //Yaxis top value
        long ptop = graph->valueAxis()->axisRect()->top();         //graph top margin
        long pbottom = graph->valueAxis()->axisRect()->bottom();   //graph bottom position
// result for Y axis
        double valueY = (evt->pos().y() - ptop) / (double)(pbottom - ptop)*(double)(dbottom - dtop) + dtop;

//or shortly for X-axis
        double valueX = (evt->pos().x() - graph->keyAxis()->axisRect()->left());  //graph width in pixels
        double ratio = (double)(graph->keyAxis()->axisRect()->right() - graph->keyAxis()->axisRect()->left()) / (double)(graph->keyAxis()->range().lower - graph->keyAxis()->range().upper);    //ratio px->graph width
//and result for X-axis
        valueX=-valueX / ratio + graph->keyAxis()->range().lower;


        qDebug()<<"calculated:"<<valueX<<valueY;
        }
}
于 2014-06-11T17:15:02.300 回答