使用Qt的QChartView绘制折线图时,无法显示折线所代表的类别名称?

When using Qt's QChartView to draw a line chart, the category name represented by the line cannot be displayed?

#include <QApplication>
#include <QMainWindow>
#include <QDateTime>
#include <QChartView>
#include <QLineSeries>
QT_CHARTS_USE_NAMESPACE

int main(int argc, char **argv) {
    QApplication application(argc, argv);

    auto *series01 = new QLineSeries();
    series01->setName("line01");
    ...

    auto *series02 = new QLineSeries();
    series02->setName("line02");
    ...

    auto *chart = new QChart();
    chart->legend()->hide();
    chart->addSeries(series01);
    chart->addSeries(series02);
    series01->setUseOpenGL(true);
    chart->createDefaultAxes();
    chart->setTitle(QStringLiteral("Qt line chart example"));

    auto *view = new QChartView(chart, nullptr);
    view->setRenderHint(QPainter::Antialiasing);
    view->resize(1000, 600);
    view->show();

    return QApplication::exec();
}

当前折线图如下所示:

如何显示这两行各自的名称?

只需要显示他们的名字,颜色对应即可,位置和形状不需要。

发生这种情况是因为您隐藏了 legend

#include <QApplication>
#include <QMainWindow>
#include <QDateTime>
#include <QChartView>
#include <QLineSeries>

QT_CHARTS_USE_NAMESPACE

int  main(int argc, char **argv)
{
    QApplication  application(argc, argv);
    auto         *series01 = new QLineSeries();

    series01->setName("line01");

    series01->append(0, 0);
    series01->append(2, 5.5);
    series01->append(3.2, 8);
    series01->append(8, 9);
    series01->append(11, 4);

    auto *series02 = new QLineSeries();
    series02->setName("line02");

    series02->append(0, 0);
    series02->append(1.5, 2);
    series02->append(4, 4);
    series02->append(6, 6);
    series02->append(7.5, 8);
    series02->append(7.5, 1);
    series02->append(8.1, 5);

    auto *chart = new QChart();
// chart->legend()->hide();
    chart->addSeries(series01);
    chart->addSeries(series02);
    series01->setUseOpenGL(true);
    chart->createDefaultAxes();
    chart->setTitle(QStringLiteral("Qt line chart example"));

    auto *view = new QChartView(chart, nullptr);
    view->setRenderHint(QPainter::Antialiasing);
    view->resize(1000, 600);
    view->show();

    return QApplication::exec();
}