赞
踩
对上一篇的工作C++学习笔记 | 基于Qt框架开发实时成绩显示排序系统1-CSDN博客继续优化,增加一个显示运动员每组成绩的折线图。
.pro
文件)中添加对Qt Charts模块的支持:QT += charts
- public:
- // 获取所有运动员的列表
- std::vector<Athlete> getAthletes() const;
- std::vector<Athlete> AthleteModel::getAthletes() const {
- std::vector<Athlete> athletesList;
- for (int row = 0; row < rowCount(); ++row) {
- Athlete athlete;
- athlete.name = item(row, 0)->text().toStdString();
- for (int col = 1; col <= 6; ++col) { // 假设前6列是成绩
- athlete.scores[col - 1] = item(row, col)->text().toFloat();
- }
- athlete.totalScore = item(row, 7)->text().toFloat(); // 假设第7列是总分
- athletesList.push_back(athlete);
- }
- return athletesList;
- }
- //其他内容....
- #include <QtCharts>
- using namespace QtCharts;
-
- //其他代码....
-
-
- QAction* actBtn = new QAction(QIcon(":/ZXT.png"), "折线图");
- ui->actionBtn->setDefaultAction(actBtn);
- connect(ui->actionBtn, &QToolButton::triggered, this, [=]() {
- QChart *chart = new QChart();
- chart->setTitle("运动员成绩折线图");
- chart->legend()->setVisible(true);
- chart->legend()->setAlignment(Qt::AlignBottom);
- // 获取所有运动员的列表
- auto athletes = model->getAthletes();
- for (const auto &athlete : athletes) {
- QLineSeries *series = new QLineSeries();
- series->setName(QString::fromStdString(athlete.name));
- for (int i = 0; i < 6; ++i) { // 假设有6次成绩
- series->append(i + 1, athlete.scores[i]); // 添加每次成绩到序列
- }
- chart->addSeries(series);
- }
- chart->createDefaultAxes();
- if (!athletes.empty()) {
- // 假设所有运动员至少有一次成绩,设置水平轴范围为1到6
- chart->axes(Qt::Horizontal).first()->setRange(1, 6);
- // 这里需要确定垂直轴的合适范围
- float maxScore = 0;
- float minScore = 200;
- for (const auto &athlete : athletes) {
- for (float score : athlete.scores) {
- if (score > maxScore) maxScore = score;
- if (score < minScore) minScore = score;
- }
- }
- chart->axes(Qt::Vertical).first()->setRange(minScore, maxScore);
- }
- QChartView *chartView = new QChartView(chart);
- chartView->setRenderHint(QPainter::Antialiasing);
-
- // 创建一个新窗口显示这个图表
- QMainWindow *chartWindow = new QMainWindow();
- chartWindow->setCentralWidget(chartView);
- chartWindow->resize(1000, 500);
- chartWindow->setWindowIcon(QIcon(":/ZXT.png"));
- chartWindow->setWindowTitle("成绩折线图");
- chartWindow->show();
- });
- ui->actionBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
-
-
- //其他代码....
参考这篇博文:
QT导出安装文件的方法(WIN和Andriod平台)_qt怎么导出项目-CSDN博客
windeployqt study_Qt.exe
我的下一篇博文对该程序继续优化,增加了保存按钮:C++ Qt框架开发|基于Qt框架开发实时成绩显示排序系统(3) 保存表格数据-CSDN博客
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。