QObject::connect(ui.radioButton, &QRadioButton::toggled, [&]() { if (!ui.radioButton->isChecked()) { QStringList dataList; for (int i = 0; i < 30; i++) { int k = rand() % 10; dataList.append(QString::number(k)); } QChart* m_chart; QLineSeries* lineSeries = new QLineSeries(); QChartView* chartView = new QChartView(this); for (quint8 i = 0; i < dataList.length() - 1; i++) { qreal x = i; qreal y = dataList[i].toInt(); lineSeries->append(x, y); } lineSeries->setName("case 1"); m_chart = new QChart(); m_chart->addSeries(lineSeries); m_chart->createDefaultAxes(); m_chart->setTheme(QChart::ChartThemeDark); m_chart->legend()->hide(); chartView->setChart(m_chart); chartView->resize(QSize(500, 800)); chartView->setRenderHints(QPainter::Antialiasing); qDeleteAll(ui.widget->children()); m_chart->setTitle("Audio"); QGridLayout* baseLayout = new QGridLayout(); //便于显示,创建网格布局 chartView->setRenderHint(QPainter::Antialiasing); baseLayout->addWidget(chartView, 0, 0); ui.widget->setLayout(baseLayout); } else { QBarSet* set0 = new QBarSet("Jane"); QBarSet* set1 = new QBarSet("John"); QBarSet* set2 = new QBarSet("Axel"); QBarSet* set3 = new QBarSet("Mary"); QBarSet* set4 = new QBarSet("Samantha"); *set0 << 1 << 2 << 3 << 4 << 5 << 6;// Jane 6个月份的值 *set1 << 5 << 0 << 0 << 4 << 0 << 7; *set2 << 3 << 5 << 8 << 19 << 8 << 5; *set3 << 5 << 6 << 7 << 3 << 4 << 5; *set4 << 9 << 7 << 5 << 3 << 1 << 2; //![1] //![2] QBarSeries* series = new QBarSeries(); series->append(set0); series->append(set1); series->append(set2); series->append(set3); series->append(set4); //![2] //![3] QChart* chart = new QChart(); chart->addSeries(series); chart->setTitle("Simple barchart example"); chart->setAnimationOptions(QChart::SeriesAnimations); //![3] //![4] QStringList categories; categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; QBarCategoryAxis* axis = new QBarCategoryAxis(); axis->append(categories); chart->createDefaultAxes();//创建默认的左侧的坐标轴(根据 QBarSet 设置的值) chart->setAxisX(axis, series);//设置坐标轴 //![4] //![5] chart->legend()->setVisible(true); //设置图例为显示状态 chart->legend()->setAlignment(Qt::AlignBottom);//设置图例的显示位置在底部 //![5] //![6] QChartView* chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); //![6] //![7] qDeleteAll(ui.widget->children()); chart->setTitle("Audio"); QGridLayout* baseLayout = new QGridLayout(); //便于显示,创建网格布局 chartView->setRenderHint(QPainter::Antialiasing); baseLayout->addWidget(chartView, 0, 0); ui.widget->setLayout(baseLayout); } } });