c++ - 如何在两个图形的工具提示上显示键/值对?

标签 c++ qt qcustomplot

我正在使用 QCustomPlot 来显示图表,每个图表都有不同的键/值对。在鼠标悬停时,我希望看到每个图表的键/值对,但相反,我看到的是右侧的第一个图表,如图所示

我怎样才能让它更好地工作?

我期望的行为

当鼠标光标放在图表上时,图表会显示每个图表的键/值对。

到目前为止我尝试了什么

信号/槽连接

connect(this,&QCustomPlot::mouseMove,this,&ChartWidget::showToolTip);

showToolTip 插槽实现

   void ChartWidget::showToolTip(QMouseEvent *event){
      double x = xAxis->pixelToCoord(event->pos().x());
      double y = yAxis->pixelToCoord(event->pos().y());

     if(x>0&&y>0 && x<xAxis->range().upper) 
     setToolTip(tr("%1,%2").arg(x).arg(y));}

最佳答案

您要做的是验证 QCPAxisRect 是否在光标上方。然后获取每个 QCPAxisRect 的轴和值:

void showToolTip(QMouseEvent *event){
    for(QCPLayoutElement *element : plotLayout()->elements(true)){
        QCPAxisRect *axisRect = static_cast<QCPAxisRect *>(element);
        if(axisRect){
            if(axisRect->rect().contains(event->pos())){
                double x= axisRect->axis(QCPAxis::atBottom)->pixelToCoord(event->x());
                double y= axisRect->axis(QCPAxis::atLeft)->pixelToCoord(event->y());
                setToolTip(tr("%1,%2").arg(x).arg(y));
                break;
            }
        }
    }
}

例子:

#include "qcustomplot.h"

#include <QApplication>

class ChartWidget: public QCustomPlot{
public:
    ChartWidget(QWidget *parent=nullptr):QCustomPlot(parent){

        plotLayout()->clear();
        QCPAxisRect *leftAxisRect = new QCPAxisRect(this);
        QCPAxisRect *rightAxisRect = new QCPAxisRect(this);
        plotLayout()->addElement(0, 0, leftAxisRect);
        plotLayout()->addElement(0, 1, rightAxisRect);

        QVector<QCPGraphData> dataCos(100);

        for(int i=0; i<dataCos.size(); ++i){
            dataCos[i].key = i/(double)(dataCos.size()-1)*10-5.0;
            dataCos[i].value = qCos(dataCos[i].key);
        }

        QCPGraph *mainGraphCos = addGraph(leftAxisRect->axis(QCPAxis::atBottom), leftAxisRect->axis(QCPAxis::atLeft));
        mainGraphCos->data()->set(dataCos);
        mainGraphCos->valueAxis()->setRange(-1, 1);
        mainGraphCos->rescaleKeyAxis();
        mainGraphCos->setPen(QPen(QColor("blue"), 2));

        QVector<QCPGraphData> dataExp(100);

        for(int i=0; i<dataExp.size(); ++i){
            dataExp[i].key = i/(double)(dataExp.size()-1)*10-5.0;
            dataExp[i].value = qExp(dataExp[i].key)*qCos(dataExp[i].key);
        }

        QCPGraph *mainGraphExp = addGraph(rightAxisRect->axis(QCPAxis::atBottom), rightAxisRect->axis(QCPAxis::atLeft));
        mainGraphExp->data()->set(dataExp);
        mainGraphExp->keyAxis()->setRange(-5, 5);
        mainGraphExp->rescaleValueAxis();
        mainGraphExp->setPen(QPen(QColor("red"), 2));

        connect(this, &ChartWidget::mouseMove, this, &ChartWidget::showToolTip);
    }
private:
    void showToolTip(QMouseEvent *event){
        for(QCPLayoutElement *element : plotLayout()->elements(true)){
            QCPAxisRect *axisRect = static_cast<QCPAxisRect *>(element);
            if(axisRect){
                if(axisRect->rect().contains(event->pos())){
                    double x= axisRect->axis(QCPAxis::atBottom)->pixelToCoord(event->x());
                    double y= axisRect->axis(QCPAxis::atLeft)->pixelToCoord(event->y());
                    setToolTip(tr("%1,%2").arg(x).arg(y));
                    break;
                }
            }
        }
    }
};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    ChartWidget w;
    w.resize(640, 480);
    w.show();

    return a.exec();
}

在下面link你可以找到完整的例子

关于c++ - 如何在两个图形的工具提示上显示键/值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51173216/

相关文章:

c++ - 如何设置 Xcode 来代替 Qt Creator 工作?

c++ - 在 ubuntu 上配置 Qt 4.6.2 时出错 - "You don' t 似乎在您的 PATH 中有 'make' 或 'gmake'。无法继续”

c++ - QCustomPlot 填充连接线

c++ - QCustomPlot 与图形上的单个点交互

c++ - 加载和保存带有波兰语字符的 HTML 文件

c++ - windows平台错误如何解决

c++ - 当 C++ 状态上的绑定(bind)变量更改时,QML 自定义项状态不会更改

qt - QML:如何获取中继器的总高度?

qt - 与 Qwt 相比,QCustomPlot 的优点/缺点是什么?

c++ - 在C++中使用XGBoost时的 undefined symbol