gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:14:24 30 4
gpt4 key购买 nike

我正在使用 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com