当前位置:网站首页>Qchart note 2: add rollover display

Qchart note 2: add rollover display

2022-06-27 04:08:00 gdizcm

Pick up note 1. After the line is displayed , I want to hover over the line , Displays the... Of the point Y The value of the shaft . Like the following figure .

The implementation is like this . Inherit first QChartView class , Add a slot function to the class , Used to connect LineSeries Hover signal on .

QObject::connect(series, SIGNAL(hovered(const QPointF&,bool)), chartView, SLOT(showPos(const QPointF&, bool)));
class ChartView : public QChartView
{
    Q_OBJECT
public:
    ChartView(QChart *chart, QWidget *parent = 0);
public slots:
    void showPos(const QPointF&, bool);
};

void ChartView::showPos(const QPointF& point, bool state)
{   
    QPoint tempPoint;
    tempPoint.setX(qRound(point.x()));
    tempPoint.setY(qRound(point.y()));
    if (state && static_cast<QSplineSeries*>(this->chart()->series().at(0))->points().contains(tempPoint)) {
        QToolTip::showText(QCursor::pos(), QString("%1").arg(tempPoint.y()), this);
    }
}

In this slot function , The integer part of the first value , Because the point is the value of an integer , Then judge whether the point is LineSeries On , If you're here , Displayed at the mouse position .

Allied , With a little modification, you can also display x The value of the shaft .

原网站

版权声明
本文为[gdizcm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270356181728.html