首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将鼠标悬停在上方时显示绘图值。-检测散点

问将鼠标悬停在上方时显示绘图值。-检测散点
EN

Stack Overflow用户
提问于 2013-08-09 13:19:58
回答 3查看 13.1K关注 0票数 10

我正试图在我的QCustomPlot上显示不同点的绘图值,在我的the中,我的线型为lsLine。我知道我可以在QCustomPlot上设置一个鼠标经过的信号,但这不会有什么帮助,因为我只需要在鼠标经过我绘制的line.My时得到通知,我的问题是有什么方法可以找出鼠标是否在我的散点上。有没有我可以连接的信号,当鼠标经过散点时,它会告诉我?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-11-11 02:48:06

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

然后使用axes的coordToPixel平移(光标)像素坐标来绘制坐标,并使用QMap::lowerBound(cursorX)在QCPDataMap中搜索最近的点。

票数 9
EN

Stack Overflow用户

发布于 2014-05-14 20:48:17

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

代码语言:javascript
复制
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));

}
票数 10
EN

Stack Overflow用户

发布于 2014-06-12 01:15:02

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

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

代码语言:javascript
复制
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;
        }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18140446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档