【问题标题】:Display the plot values on mouse over. - Detect Scatter points在鼠标悬停时显示绘图值。 - 检测散点
【发布时间】:2013-08-11 00:24:42
【问题描述】:

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

【问题讨论】:

    标签: c++ qt mouseover point qcustomplot


    【解决方案1】:

    您可以轻松地将插槽连接到QCustomPlot 发出的mouseMove 信号。然后您可以使用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));
    
    }
    

    【讨论】:

    • 如果我在 ui 上有两个图表,ui->widget_graph1ui->widget_graph2,我怎么能同时处理这两个图表?我应该更改函数名称void CustomPlot::showPointToolTip(QMouseEvent *event){} 以适应我的情况吗?我希望两个图表都显示鼠标悬停时的坐标,谢谢
    • @Wei 如果您像我一样在QCustomPlot 源代码中实现插槽,则会显示所有绘图的工具提示。您也可以将插槽放在另一个类中,并使用sender() 找出发出mouseMove 信号的图。
    • 我只找到QCustomPlot::toolTip,把你的void QCustomPlot::showPointToolTip(QMouseEvent *event){}改成void QCustomPlot::toolTip(QMouseEvent *event){}会不会一样?
    【解决方案2】:

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

    然后使用轴的coordToPixel 转换(光标)像素坐标以绘制坐标并使用QMap::lowerBound(cursorX)QCPDataMap 中搜索最近的点。

    【讨论】:

    • @Rajeshwar coordToPixel 将绘图坐标转换为像素坐标。它是如何解决您的问题的?
    【解决方案3】:

    当您使用 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;
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      相关资源
      最近更新 更多