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 44
|
||||||
\snippet mainwindows/application/mainwindow.cpp 45
|
\snippet mainwindows/application/mainwindow.cpp 45
|
||||||
|
|
||||||
Saving a file is very similar to loading one. Here, the
|
Saving a file is similar to loading one. We use QSaveFile to ensure
|
||||||
QFile::Text flag ensures that on Windows, "\\n" is converted into
|
all data are safely written and existing files are not damaged
|
||||||
"\\r\\n" to conform to the Windows convension.
|
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 46
|
||||||
\snippet mainwindows/application/mainwindow.cpp 47
|
\snippet mainwindows/application/mainwindow.cpp 47
|
||||||
|
@ -353,24 +353,28 @@ void MainWindow::loadFile(const QString &fileName)
|
|||||||
bool MainWindow::saveFile(const QString &fileName)
|
bool MainWindow::saveFile(const QString &fileName)
|
||||||
//! [44] //! [45]
|
//! [44] //! [45]
|
||||||
{
|
{
|
||||||
QFile file(fileName);
|
QString errorMessage;
|
||||||
if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
|
||||||
QMessageBox::warning(this, tr("Application"),
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
||||||
tr("Cannot write file %1:\n%2.")
|
QSaveFile file(fileName);
|
||||||
.arg(QDir::toNativeSeparators(fileName),
|
if (file.open(QFile::WriteOnly | QFile::Text)) {
|
||||||
file.errorString()));
|
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;
|
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);
|
setCurrentFile(fileName);
|
||||||
statusBar()->showMessage(tr("File saved"), 2000);
|
statusBar()->showMessage(tr("File saved"), 2000);
|
||||||
return true;
|
return true;
|
||||||
|
@ -115,19 +115,28 @@ bool MdiChild::saveAs()
|
|||||||
|
|
||||||
bool MdiChild::saveFile(const QString &fileName)
|
bool MdiChild::saveFile(const QString &fileName)
|
||||||
{
|
{
|
||||||
QFile file(fileName);
|
QString errorMessage;
|
||||||
if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
|
||||||
QMessageBox::warning(this, tr("MDI"),
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
||||||
tr("Cannot write file %1:\n%2.")
|
QSaveFile file(fileName);
|
||||||
.arg(QDir::toNativeSeparators(fileName), file.errorString()));
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QTextStream out(&file);
|
|
||||||
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
|
||||||
out << toPlainText();
|
|
||||||
QGuiApplication::restoreOverrideCursor();
|
|
||||||
|
|
||||||
setCurrentFile(fileName);
|
setCurrentFile(fileName);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -425,19 +425,28 @@ void MainWindow::openRecentFile()
|
|||||||
|
|
||||||
bool MainWindow::saveFile(const QString &fileName)
|
bool MainWindow::saveFile(const QString &fileName)
|
||||||
{
|
{
|
||||||
QFile file(fileName);
|
QString errorMessage;
|
||||||
if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
|
||||||
QMessageBox::warning(this, tr("SDI"),
|
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
||||||
tr("Cannot write file %1:\n%2.")
|
QSaveFile file(fileName);
|
||||||
.arg(QDir::toNativeSeparators(fileName), file.errorString()));
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QTextStream out(&file);
|
|
||||||
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
|
|
||||||
out << textEdit->toPlainText();
|
|
||||||
QGuiApplication::restoreOverrideCursor();
|
|
||||||
|
|
||||||
setCurrentFile(fileName);
|
setCurrentFile(fileName);
|
||||||
statusBar()->showMessage(tr("File saved"), 2000);
|
statusBar()->showMessage(tr("File saved"), 2000);
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user