【问题标题】:How to use QMouseEvent to display the mouse position in a QChart?如何使用 QMouseEvent 在 QChart 中显示鼠标位置?
【发布时间】:2020-10-27 11:07:38
【问题描述】:

我对 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()));

}

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);

我试图像 Qt Callout Example 那样编辑我的代码。
我得到的错误: 'virtual void QChart_ME::mouseMoveEvent(QGraphicsSceneMouseEvent*)' 在此上下文中受到保护 this->chartTrendLine->mouseMoveEvent(myEvent);

我该如何解决这个问题?

【问题讨论】:

  • boxWhisker.cpp 是否只包含一行?
  • @scophanov 没有。其余的代码在上面。我没有改变休息。我只改了一行。这是编辑的部分。
  • 请使示例完整,因此无需假设,也无需修改即可运行。
  • 关于您的编辑,请注意 QChart 是在命名空间 QT_CHARTS_NAMESPACE 中定义的,因此您需要将用法限定为 QT_CHARTS_NAMESPACE::QChart 或使用 QT_CHARTS_USE_NAMESPACE 宏。这至少应该消除expected class-name before... 编译器错误。
  • @G.M.我编辑了我的问题并添加了完整的代码。如果你愿意,你可以检查!

标签: c++ qt qchart qmouseevent


【解决方案1】:

原因

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

解决方案

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

【讨论】:

  • @scophanov 我尝试继承 QChart 类并创建一个新类并将鼠标事件功能添加到新类,但 QChart 类无法继承。这是我的代码示例: class QChart_ME : public QChart { public: QChart_ME();无效鼠标事件(); };
  • @trytobedeveloper,我现在的机器上没有 Qt Charts 模块,但是我没有看到阻止 QChart 子类化的原因。你得到什么错误?
  • @scophanov 我包含了 并且它基本上看不到我的 QChart 关键字,并给了我错误“在“{”令牌“之前的预期类名”
  • 我无法像这样弄清楚。请准备一个Minimal, Reproducible Example
  • 我编辑了我的帖子,您可以查看
猜你喜欢
  • 1970-01-01
  • 2022-06-10
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多