Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用QMouseEvent在QChart中显示鼠标位置? 我在QT和C++方面很新。我有一个QChart,它有一个QLineSeries对象。我想向用户展示鼠标在坐标系上的投影。我的问题是,除了QChart对象,我可以在任何地方显示坐标。我只想在鼠标位于QChart上时显示坐标。以下是我的代码示例:_C++_Qt_Qchart_Qmouseevent - Fatal编程技术网

如何使用QMouseEvent在QChart中显示鼠标位置? 我在QT和C++方面很新。我有一个QChart,它有一个QLineSeries对象。我想向用户展示鼠标在坐标系上的投影。我的问题是,除了QChart对象,我可以在任何地方显示坐标。我只想在鼠标位于QChart上时显示坐标。以下是我的代码示例:

如何使用QMouseEvent在QChart中显示鼠标位置? 我在QT和C++方面很新。我有一个QChart,它有一个QLineSeries对象。我想向用户展示鼠标在坐标系上的投影。我的问题是,除了QChart对象,我可以在任何地方显示坐标。我只想在鼠标位于QChart上时显示坐标。以下是我的代码示例:,c++,qt,qchart,qmouseevent,C++,Qt,Qchart,Qmouseevent,h文件 QGraphicsSimpleTextItem *m_coordX; QGraphicsSimpleTextItem *m_coordY; QChart *chartTrendLine; QChartView *trendLineChartView; QLineSeries *trendLine; boxwhisbr.cpp文件 this->chartTrendLine = new QChart(); this->chartTrendLine->addSeries(t

h文件

QGraphicsSimpleTextItem *m_coordX;
QGraphicsSimpleTextItem *m_coordY;
QChart *chartTrendLine;
QChartView *trendLineChartView;
QLineSeries *trendLine;
boxwhisbr.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()));

}
boxWhisker.h:

QChart_ME *chartTrendLine; 
Boxwhisbr.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标注示例那样编辑代码。
我得到的错误是: “virtual void QChart_ME::mouseMoveEvent(QGraphicsSceneMouseEvent*)”在此上下文中受保护 此->图表趋势线->鼠标移动事件(myEvent)

如何解决此问题?

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

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

boxWhisker.cpp
只包含一行吗?@scophanov否。其余的代码在上面。我没有更改其余部分。我只更改了一行。这是编辑过的部分。请完成示例,因此没有假设,也没有修改需要运行它。关于编辑,请注意,
QChart
是在名称空间
QT\u CHARTS\u名称空间
中定义的,因此您需要将用法限定为
QT\u CHARTS\u名称空间::QChart
或使用
QT\u CHARTS\u use\u名称空间
宏。这至少应该在…
编译器错误。@G.M.我编辑了我的问题并添加了完整的代码。如果需要,您可以检查!