summaryrefslogtreecommitdiffstats
path: root/examples/widgets/mainwindows
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/mainwindows')
-rw-r--r--examples/widgets/mainwindows/application/mainwindow.cpp34
-rw-r--r--examples/widgets/mainwindows/mdi/mainwindow.cpp2
-rw-r--r--examples/widgets/mainwindows/mdi/mdichild.cpp27
-rw-r--r--examples/widgets/mainwindows/sdi/mainwindow.cpp31
4 files changed, 58 insertions, 36 deletions
diff --git a/examples/widgets/mainwindows/application/mainwindow.cpp b/examples/widgets/mainwindows/application/mainwindow.cpp
index 4b639ead18..d0c009427f 100644
--- a/examples/widgets/mainwindows/application/mainwindow.cpp
+++ b/examples/widgets/mainwindows/application/mainwindow.cpp
@@ -281,7 +281,7 @@ void MainWindow::readSettings()
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
if (geometry.isEmpty()) {
- const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
+ const QRect availableGeometry = screen()->availableGeometry();
resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
move((availableGeometry.width() - width()) / 2,
(availableGeometry.height() - height()) / 2);
@@ -353,23 +353,27 @@ 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()));
- return false;
- }
+ QString errorMessage;
- QTextStream out(&file);
-#ifndef QT_NO_CURSOR
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
-#endif
- out << textEdit->toPlainText();
-#ifndef QT_NO_CURSOR
+ 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();
-#endif
+
+ if (!errorMessage.isEmpty()) {
+ QMessageBox::warning(this, tr("Application"), errorMessage);
+ return false;
+ }
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
diff --git a/examples/widgets/mainwindows/mdi/mainwindow.cpp b/examples/widgets/mainwindows/mdi/mainwindow.cpp
index b952d19e2e..ccfa7435d7 100644
--- a/examples/widgets/mainwindows/mdi/mainwindow.cpp
+++ b/examples/widgets/mainwindows/mdi/mainwindow.cpp
@@ -464,7 +464,7 @@ void MainWindow::readSettings()
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
if (geometry.isEmpty()) {
- const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
+ const QRect availableGeometry = screen()->availableGeometry();
resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
move((availableGeometry.width() - width()) / 2,
(availableGeometry.height() - height()) / 2);
diff --git a/examples/widgets/mainwindows/mdi/mdichild.cpp b/examples/widgets/mainwindows/mdi/mdichild.cpp
index 16f2040de0..727d4f6cfd 100644
--- a/examples/widgets/mainwindows/mdi/mdichild.cpp
+++ b/examples/widgets/mainwindows/mdi/mdichild.cpp
@@ -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()));
- return false;
- }
+ QString errorMessage;
- QTextStream out(&file);
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
- out << toPlainText();
+ 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;
+ }
+
setCurrentFile(fileName);
return true;
}
diff --git a/examples/widgets/mainwindows/sdi/mainwindow.cpp b/examples/widgets/mainwindows/sdi/mainwindow.cpp
index 62a74b26e6..c3cd131923 100644
--- a/examples/widgets/mainwindows/sdi/mainwindow.cpp
+++ b/examples/widgets/mainwindows/sdi/mainwindow.cpp
@@ -167,7 +167,7 @@ void MainWindow::tile(const QMainWindow *previous)
if (!topFrameWidth)
topFrameWidth = 40;
const QPoint pos = previous->pos() + 2 * QPoint(topFrameWidth, topFrameWidth);
- if (QApplication::desktop()->availableGeometry(this).contains(rect().bottomRight() + pos))
+ if (screen()->availableGeometry().contains(rect().bottomRight() + pos))
move(pos);
}
@@ -290,7 +290,7 @@ void MainWindow::readSettings()
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
if (geometry.isEmpty()) {
- const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
+ const QRect availableGeometry = screen()->availableGeometry();
resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
move((availableGeometry.width() - width()) / 2,
(availableGeometry.height() - height()) / 2);
@@ -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;