如何在两个图表上的工具提示上显示键/值对?

时间:2018-07-04 12:01:12

标签: 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));}

1 个答案:

答案 0 :(得分:1)

您要做的是验证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中,您可以找到完整的示例