對(duì)上兩篇篇的工作C++ Qt框架開發(fā)| 基于Qt框架開發(fā)實(shí)時(shí)成績(jī)顯示排序系統(tǒng)(1)-CSDN博客和C++ Qt框架開發(fā) | 基于Qt框架開發(fā)實(shí)時(shí)成績(jī)顯示排序系統(tǒng)(2)折線圖顯示-CSDN博客繼續(xù)優(yōu)化,增加一個(gè)保存按鈕,用于保存成績(jī)數(shù)據(jù)。
1)在ui界面添加一個(gè)按鈕
? ? ? ? 將其命名為saveBtn。文章來源:http://www.zghlxwxcb.cn/news/detail-825635.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-825635.html
2)在mainwindow.cpp中添加如下槽函數(shù)
QAction* sBtn = new QAction("保存");
ui->saveBtn->setDefaultAction(sBtn);
connect(ui->saveBtn, &QToolButton::triggered, this, [=]() {
// 彈出保存文件對(duì)話框
QString filePath = QFileDialog::getSaveFileName(this, tr("保存成績(jī)"), "", tr("CSV文件 (*.csv)"));
if (filePath.isEmpty()) return; // 用戶取消操作
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("保存失敗"), tr("無法打開文件進(jìn)行寫入"));
return;
}
QTextStream out(&file);
// 寫入標(biāo)題行
QStringList headers;
for (int column = 0; column < proxyModel->columnCount(); ++column) {
headers << proxyModel->headerData(column, Qt::Horizontal).toString();
}
out << headers.join(",") + "\n";
// 寫入數(shù)據(jù)行
for (int row = 0; row < proxyModel->rowCount(); ++row) {
QStringList rowItems;
for (int column = 0; column < proxyModel->columnCount(); ++column) {
QModelIndex index = proxyModel->index(row, column);
rowItems << proxyModel->data(index).toString();
}
out << rowItems.join(",") + "\n";
}
file.close(); // 關(guān)閉文件
QMessageBox::information(this, tr("保存成功"), tr("成績(jī)表格已成功保存。"));
});
到了這里,關(guān)于C++ Qt框架開發(fā) | 基于Qt框架開發(fā)實(shí)時(shí)成績(jī)顯示排序系統(tǒng)(3) 保存表格數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!