Qt保存文件
#include <QApplication> #include <QFileDialog> #include <QFile> #include <QTextStream> #include <QMessageBox> void saveFileWithDialog() { QString fileName = QFileDialog::getSaveFileName(nullptr, "保存文件", "", "文本文件 (*.txt)"); if (fileName.isEmpty()) { // 用户取消了保存操作 return; } QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { QMessageBox::warning(nullptr, "错误", "无法保存文件:" + file.errorString()); return; } QTextStream out(&file); out << "这是一些要保存的内容"; file.close(); } int main(int argc, char *argv[]) { QApplication app(argc, argv); saveFileWithDialog(); return app.exec(); }
##########################
QQ 3087438119