我对Qt和C++非常陌生。我有一个QChart,它有一个QLineSeries对象。我想向用户展示鼠标在坐标系上的投影。我的问题是,除了我的QChart对象之外,我可以在任何地方显示坐标。我只想在鼠标在QChart时显示坐标。下面是我的代码示例:
boxWhisker.h文件
QGraphicsSimpleTextItem *m_coordX;
QGraphicsSimpleTextItem *m_coordY;
QChart *chartTrendLine;
QChartView *trendLineChartView;
QLineSeries *trendLine;boxWhisker.cpp文件
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:
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()));
}盒语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);我试图像Qt标注示例那样编辑我的代码。
我得到的错误:‘虚拟空QChart_ME::mouseMoveEvent(QGraphicsSceneMouseEvent*)’在这个上下文this->chartTrendLine->mouseMoveEvent(myEvent);中受到保护
我怎样才能解决这个问题?
发布于 2020-10-27 11:15:03
原因
您正在为您的boxWhiskerDialog类而不是为QChart获取鼠标事件。
解决方案
子类QChart并重新实现其mouseMoveEvent,而不是重新实现boxWhiskerDialog::mouseMoveEvent。
https://stackoverflow.com/questions/64553129
复制相似问题