I am pretty new on Qt and C++. I have a QChart which has a QLineSeries object. I want to show the user the projection of the mouse on the coordinate system. My problem is I can display coordinates everywhere except my QChart object. I want to display coordinates only when mouse is on QChart. Here is the sample of my code :
boxWhisker.h file
QGraphicsSimpleTextItem *m_coordX;
QGraphicsSimpleTextItem *m_coordY;
QChart *chartTrendLine;
QChartView *trendLineChartView;
QLineSeries *trendLine;
boxWhisker.cpp file
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()));
}
My question is how can I display coordinates only on QChart? Any help will be appriciated thanks!
EDIT
Here I tried to create a new class which inherited by QChart class and define my mouseEvent function in my new class. Here is the sample of my code :
qchart_me.h :
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 :
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()));
}
boxWhisker.h:
QChart_ME *chartTrendLine;
boxWhisker.cpp
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);
I was trying to edit my code like Qt Callout Example.
The error I get :
'virtual void QChart_ME::mouseMoveEvent(QGraphicsSceneMouseEvent*)' is protected within this context
this->chartTrendLine->mouseMoveEvent(myEvent);
How can I solve this issue?