From 20901898a853ca898eed1a41c3e2f2024256ea86 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Tue, 29 Jan 2019 20:55:16 +0100 Subject: [PATCH] Show and handle load and save errors --- source/DataModel.cpp | 4 ++-- source/mainwindow.cpp | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/source/DataModel.cpp b/source/DataModel.cpp index eba7f53..eef2e2f 100644 --- a/source/DataModel.cpp +++ b/source/DataModel.cpp @@ -46,7 +46,7 @@ void DataModel::write(const QString &filename) const QFile outFile(filename); if (!outFile.open(QIODevice::WriteOnly)) { - return; + throw std::runtime_error("open failed"); } dataModel.SerializeToFileDescriptor(outFile.handle()); @@ -57,7 +57,7 @@ void DataModel::read(const QString &filename) QFile inFile(filename); if (!inFile.open(QIODevice::ReadOnly)) { - return; + throw std::runtime_error("open failed"); } ESGRAF48::DataModel dataModel; diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index 8f0eab0..aa6a915 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -85,7 +85,15 @@ void MainWindow::openFile() return; } - openFile(filename); + try + { + openFile(filename); + } + catch (std::exception &e) + { + QString errorMessage = QString("Error loading \"") + filename + "\": " + e.what(); + QMessageBox::critical(this, "Error", errorMessage); + } } void MainWindow::openFile(const QString &filename) @@ -195,7 +203,16 @@ void MainWindow::closeEvent(QCloseEvent *event) void MainWindow::saveFile(const QString &filename) { - m_dataModel.write(filename); + try + { + m_dataModel.write(filename); + } + catch (std::exception &e) + { + QString errorMessage = QString("Error saving \"") + filename + "\": " + e.what(); + QMessageBox::critical(this, "Error", errorMessage); + return; + } qDebug() << "Wrote" << filename;