Use QSaveFile in MainWindow examples
QSaveFile should preferably be used by editor applications to catch write errors. Task-number: QTBUG-60635 Change-Id: Ia609435871b56b45714c3dd3d32bbc85b5cb4dd5 Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
parent
0ad5e16268
commit
8c9e41cc78
@ -327,9 +327,12 @@
|
||||
\snippet mainwindows/application/mainwindow.cpp 44
|
||||
\snippet mainwindows/application/mainwindow.cpp 45
|
||||
|
||||
Saving a file is very similar to loading one. Here, the
|
||||
QFile::Text flag ensures that on Windows, "\\n" is converted into
|
||||
"\\r\\n" to conform to the Windows convension.
|
||||
Saving a file is similar to loading one. We use QSaveFile to ensure
|
||||
all data are safely written and existing files are not damaged
|
||||
should writing fail.
|
||||
We use the QFile::Text flag to make sure that on Windows, "\\n"
|
||||
is converted into "\\r\\n" to conform to the Windows convention.
|
||||
|
||||
|
||||
\snippet mainwindows/application/mainwindow.cpp 46
|
||||
\snippet mainwindows/application/mainwindow.cpp 47
|
||||
|
@ -353,24 +353,28 @@ void MainWindow::loadFile(const QString &fileName)
|
||||
bool MainWindow::saveFile(const QString &fileName)
|
||||
//! [44] //! [45]
|
||||
{
|
||||
QFile file(fileName);
|
||||
if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
||||
QMessageBox::warning(this, tr("Application"),
|
||||
tr("Cannot write file %1:\n%2.")
|
||||
.arg(QDir::toNativeSeparators(fileName),
|
||||
file.errorString()));
|
||||
QString errorMessage;
|
||||
|
||||
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
||||
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("Application"), errorMessage);
|
||||
return false;
|
||||
}
|
||||
|
||||
QTextStream out(&file);
|
||||
#ifndef QT_NO_CURSOR
|
||||
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
||||
#endif
|
||||
out << textEdit->toPlainText();
|
||||
#ifndef QT_NO_CURSOR
|
||||
QGuiApplication::restoreOverrideCursor();
|
||||
#endif
|
||||
|
||||
setCurrentFile(fileName);
|
||||
statusBar()->showMessage(tr("File saved"), 2000);
|
||||
return true;
|
||||
|
@ -115,19 +115,28 @@ bool MdiChild::saveAs()
|
||||
|
||||
bool MdiChild::saveFile(const QString &fileName)
|
||||
{
|
||||
QFile file(fileName);
|
||||
if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
||||
QMessageBox::warning(this, tr("MDI"),
|
||||
tr("Cannot write file %1:\n%2.")
|
||||
.arg(QDir::toNativeSeparators(fileName), file.errorString()));
|
||||
QString errorMessage;
|
||||
|
||||
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
||||
QSaveFile file(fileName);
|
||||
if (file.open(QFile::WriteOnly | QFile::Text)) {
|
||||
QTextStream out(&file);
|
||||
out << 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("MDI"), errorMessage);
|
||||
return false;
|
||||
}
|
||||
|
||||
QTextStream out(&file);
|
||||
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
||||
out << toPlainText();
|
||||
QGuiApplication::restoreOverrideCursor();
|
||||
|
||||
setCurrentFile(fileName);
|
||||
return true;
|
||||
}
|
||||
|
@ -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()));
|
||||
QString errorMessage;
|
||||
|
||||
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
||||
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;
|
||||
}
|
||||
|
||||
QTextStream out(&file);
|
||||
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
||||
out << textEdit->toPlainText();
|
||||
QGuiApplication::restoreOverrideCursor();
|
||||
|
||||
setCurrentFile(fileName);
|
||||
statusBar()->showMessage(tr("File saved"), 2000);
|
||||
return true;
|
||||
|
Loading…
x
Reference in New Issue
Block a user