Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
QmitkSafeNotify.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef QmitkSafeNotify_h
14 #define QmitkSafeNotify_h
15 
16 #include <mitkException.h>
17 #include <mitkLog.h>
18 
19 #include <QMessageBox>
20 
21 #include <cstdlib>
22 
37 template <class A>
38 bool QmitkSafeNotify(A *app, QObject *receiver, QEvent *event)
39 {
40  QString msg;
41  try
42  {
43  return app->A::notify(receiver, event);
44  }
45  catch (mitk::Exception &e)
46  {
47  msg = QString("MITK Exception:\n\n") + QString("Description: ") + QString(e.GetDescription()) + QString("\n\n") +
48  QString("Filename: ") + QString(e.GetFile()) + QString("\n\n") + QString("Line: ") +
49  QString::number(e.GetLine());
50  }
51  catch (std::exception &e)
52  {
53  msg = e.what();
54  }
55  catch (...)
56  {
57  msg = "Unknown exception";
58  }
59  MITK_ERROR << "An error occurred: " << msg.toStdString();
60 
61  QMessageBox msgBox;
62  msgBox.setText("An error occurred. You should save all data and quit the program to prevent possible data loss.");
63  msgBox.setDetailedText(msg);
64  msgBox.setIcon(QMessageBox::Critical);
65  msgBox.addButton("Exit immediately", QMessageBox::YesRole);
66  msgBox.addButton("Ignore", QMessageBox::NoRole);
67 
68  int ret = msgBox.exec();
69 
70  switch (ret)
71  {
72  case 0:
73  MITK_ERROR << "The program was closed.";
74  std::exit(EXIT_FAILURE);
75  case 1:
77  << "The error was ignored by the user. The program may be in a corrupt state and don't behave like expected!";
78  break;
79  }
80 
81  return false;
82 }
83 
84 #endif
bool QmitkSafeNotify(A *app, QObject *receiver, QEvent *event)
Safely delivers a Qt event, catching and displaying any exceptions.
Base class for all MITK exceptions.
Definition: mitkException.h:53
#define MITK_ERROR
Log an error message.
Definition: mitkLog.h:372