0

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?

  • Does `boxWhisker.cpp` consist of only one line? – scopchanov Oct 27 '20 at 12:30
  • @scophanov No. The rest of the code on the up side. I haven't changed rest. I just changed one line. which is editted part. – trytobedeveloper Oct 27 '20 at 12:35
  • Please make the example complete, so no assumptions, nor modifications are needed to run it. – scopchanov Oct 27 '20 at 12:36
  • 1
    Regarding your edit, note that `QChart` is defined within the namespace `QT_CHARTS_NAMESPACE` so you need to either qualify the usage as `QT_CHARTS_NAMESPACE::QChart` or use the `QT_CHARTS_USE_NAMESPACE` macro. That should at least get rid of the `expected class-name before...` compiler error. – G.M. Oct 27 '20 at 13:20
  • @G.M. I editted my question and added complete code. If you want you can check ! – trytobedeveloper Oct 30 '20 at 06:55

1 Answers1

0

Cause

You are getting mouse events for your boxWhiskerDialog class and not for the QChart.

Solution

Subclass QChart and reimplement its mouseMoveEvent, instead of reimplementing boxWhiskerDialog::mouseMoveEvent.

scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • @scophanov I tried to inherit QChart class and create a new one and to add mouse event function to new class but QChart class is not able to inheritence. Here is the sample of my code : class QChart_ME : public QChart { public: QChart_ME(); void MouseEvent(); }; – trytobedeveloper Oct 27 '20 at 11:57
  • @trytobedeveloper, I don't have the Qt Charts module on the machine I am right now, however I don't see a reason, preventing the subclassing of QChart. What error do you get? – scopchanov Oct 27 '20 at 12:03
  • @scophanov I included and it basically doesn't see my QChart keyword and gives me the error "expected class-name before "{" token " – trytobedeveloper Oct 27 '20 at 12:08
  • I am not able to figure it out like this. Please prepare a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – scopchanov Oct 27 '20 at 12:12
  • 1
    I eddited my post you can check – trytobedeveloper Oct 27 '20 at 12:24