首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用QMouseEvent在QChart中显示鼠标位置?

如何使用QMouseEvent在QChart中显示鼠标位置?
EN

Stack Overflow用户
提问于 2020-10-27 11:07:38
回答 1查看 494关注 0票数 0

我对Qt和C++非常陌生。我有一个QChart,它有一个QLineSeries对象。我想向用户展示鼠标在坐标系上的投影。我的问题是,除了我的QChart对象之外,我可以在任何地方显示坐标。我只想在鼠标在QChart时显示坐标。下面是我的代码示例:

boxWhisker.h文件

代码语言:javascript
复制
QGraphicsSimpleTextItem *m_coordX;
QGraphicsSimpleTextItem *m_coordY;
QChart *chartTrendLine;
QChartView *trendLineChartView;
QLineSeries *trendLine;

boxWhisker.cpp文件

代码语言:javascript
复制
this->chartTrendLine = new QChart();
this->chartTrendLine->addSeries(this->trendLine);
this->chartTrendLine->legend()->setVisible(true);
this->chartTrendLine->createDefaultAxes();
this->chartTrendLine->setAcceptHoverEvents(true);

this->trendLineChartView = new QChartView(this->chartTrendLine);
this->trendLineChartView->setRenderHint(QPainter::Antialiasing);

this->m_coordX = new QGraphicsSimpleTextItem(this->chartTrendLine);
this->m_coordX->setPos(this->chartTrendLine->size().width()/2+50,this->chartTrendLine->size().height());

this->m_coordY = new QGraphicsSimpleTextItem(this->chartTrendLine);
this->m_coordY->setPos(this->chartTrendLine->size().width()/2+100,this->chartTrendLine->size().height());

void boxWhiskerDialog::mouseMoveEvent(QMouseEvent *mouseEvent)
{
this->m_coordY->setText(QString("Y: %1").arg(this->chartTrendLine->mapToValue(mouseEvent->pos()).y()));
this->m_coordX->setText(QString("X: %1").arg(this->chartTrendLine->mapToValue(mouseEvent->pos()).x()));
}

我的问题是如何只在QChart上显示坐标?任何帮助都将是徒劳无功的谢谢!

编辑

在这里,我尝试创建一个由QChart类继承的新类,并在我的新类中定义我的mouseEvent函数。下面是我的代码示例:

qchart_me.h:

代码语言:javascript
复制
class QChart_ME : public QT_CHARTS_NAMESPACE::QChart
{
public:
    QChart_ME();

protected:
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

private:
    QGraphicsSimpleTextItem *m_coordX;
    QGraphicsSimpleTextItem *m_coordY;
    QChart *m_chart;

};

qchart_me.cpp:

代码语言:javascript
复制
QChart_ME::QChart_ME()
{

}

void QChart_ME::mouseMoveEvent(QGraphicsSceneMouseEvent *Myevent)
{
    m_coordX->setText(QString("X: %1").arg(m_chart->mapToValue(Myevent->pos()).x()));
    m_coordY->setText(QString("Y: %1").arg(m_chart->mapToValue(Myevent->pos()).y()));

}

盒语h:

代码语言:javascript
复制
QChart_ME *chartTrendLine; 

boxWhisker.cpp

代码语言:javascript
复制
this->chartTrendLine = new QChart_ME();
this->chartTrendLine->addSeries(this->trendLine);
this->chartTrendLine->legend()->setVisible(true);
this->chartTrendLine->createDefaultAxes();
this->chartTrendLine->setAcceptHoverEvents(true);

QGraphicsSceneMouseEvent *myEvent;

this->chartTrendLine->mouseMoveEvent(myEvent);

我试图像Qt标注示例那样编辑我的代码。

我得到的错误:‘虚拟空QChart_ME::mouseMoveEvent(QGraphicsSceneMouseEvent*)’在这个上下文this->chartTrendLine->mouseMoveEvent(myEvent);中受到保护

我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2020-10-27 11:15:03

原因

您正在为您的boxWhiskerDialog类而不是为QChart获取鼠标事件。

解决方案

子类QChart并重新实现其mouseMoveEvent,而不是重新实现boxWhiskerDialog::mouseMoveEvent

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64553129

复制
相关文章

相似问题

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