From 8c9e41cc7803929aafb200c44276f4059b6ead6c Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 11 Jun 2019 14:20:20 +0200 Subject: Use QSaveFile in MainWindow examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QSaveFile should preferably be used by editor applications to catch write errors. Task-number: QTBUG-60635 Change-Id: Ia609435871b56b45714c3dd3d32bbc85b5cb4dd5 Reviewed-by: Paul Wicking Reviewed-by: MÃ¥rten Nordheim --- examples/widgets/mainwindows/sdi/mainwindow.cpp | 27 ++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'examples/widgets/mainwindows/sdi/mainwindow.cpp') diff --git a/examples/widgets/mainwindows/sdi/mainwindow.cpp b/examples/widgets/mainwindows/sdi/mainwindow.cpp index 62a74b26e6..a1fb42158e 100644 --- a/examples/widgets/mainwindows/sdi/mainwindow.cpp +++ b/examples/widgets/mainwindows/sdi/mainwindow.cpp @@ -425,19 +425,28 @@ void MainWindow::openRecentFile() bool MainWindow::saveFile(const QString &fileName) { - QFile file(fileName); - if (!file.open(QFile::WriteOnly | QFile::Text)) { - QMessageBox::warning(this, tr("SDI"), - tr("Cannot write file %1:\n%2.") - .arg(QDir::toNativeSeparators(fileName), file.errorString())); - return false; - } + QString errorMessage; - QTextStream out(&file); QGuiApplication::setOverrideCursor(Qt::WaitCursor); - out << textEdit->toPlainText(); + QSaveFile file(fileName); + if (file.open(QFile::WriteOnly | QFile::Text)) { + QTextStream out(&file); + out << textEdit->toPlainText(); + if (!file.commit()) { + errorMessage = tr("Cannot write file %1:\n%2.") + .arg(QDir::toNativeSeparators(fileName), file.errorString()); + } + } else { + errorMessage = tr("Cannot open file %1 for writing:\n%2.") + .arg(QDir::toNativeSeparators(fileName), file.errorString()); + } QGuiApplication::restoreOverrideCursor(); + if (!errorMessage.isEmpty()) { + QMessageBox::warning(this, tr("SDI"), errorMessage); + return false; + } + setCurrentFile(fileName); statusBar()->showMessage(tr("File saved"), 2000); return true; -- cgit v1.2.3