如何在两个图形的工具提示上显示 key/value 对?

How can I display a key/value pair on a ToolTip across two graphs?

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

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

我预期的行为

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

到目前为止我尝试了什么

Signal/Slot 连接

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你可以找到完整的例子