Переглянути джерело

ctkErrorLogStatusMessageHandler - Use QPointer to hold QMainWindow

Indeed, QMainWindow could be deleted before the message handler, using
a QPointer allows to make sure its value is always valid.
Jean-Christophe Fillion-Robin 14 роки тому
батько
коміт
ee1ab7f609

+ 19 - 16
Libs/Widgets/ctkErrorLogStatusMessageHandler.cpp

@@ -34,7 +34,7 @@ QString ctkErrorLogStatusMessageHandler::HandlerName = QLatin1String("Status");
 ctkErrorLogStatusMessageHandler::ctkErrorLogStatusMessageHandler(QMainWindow * mainWindow) :
   QObject(), ctkErrorLogAbstractMessageHandler()
 {
-  this->MainWindow = mainWindow;
+  this->MainWindow = QPointer<QMainWindow>(mainWindow);
 }
 
 // --------------------------------------------------------------------------
@@ -46,28 +46,31 @@ QString ctkErrorLogStatusMessageHandler::handlerName()const
 // --------------------------------------------------------------------------
 void ctkErrorLogStatusMessageHandler::setEnabledInternal(bool value)
 {
-  if (!this->MainWindow)
-    {
-    qCritical() << "ctkErrorLogStatusMessageHandler - "
-                   " Failed to enable ctkErrorLogStatusMessageHandler - "
-                   "QMainWindow passed to the constructor is Null !";
-    return;
-    }
-  if (!this->MainWindow->statusBar())
-    {
-    qCritical() << "ctkErrorLogStatusMessageHandler - Failed to enable the message handler: "
-                   "There is no StatusBar associated with QMainWindow" << this->MainWindow;
-    return;
-    }
   if (value)
     {
+    if (this->MainWindow.isNull())
+      {
+      qCritical() << "ctkErrorLogStatusMessageHandler - "
+                     " Failed to enable ctkErrorLogStatusMessageHandler - "
+                     "QMainWindow passed to the constructor is Null !";
+      return;
+      }
+    if (!this->MainWindow->statusBar())
+      {
+      qCritical() << "ctkErrorLogStatusMessageHandler - Failed to enable the message handler: "
+                     "There is no StatusBar associated with QMainWindow" << this->MainWindow;
+      return;
+      }
     connect(this->MainWindow->statusBar(), SIGNAL(messageChanged(QString)),
             this, SLOT(statusBarMessageChanged(QString)));
     }
   else
     {
-    disconnect(this->MainWindow->statusBar(), SIGNAL(messageChanged(QString)),
-               this, SLOT(statusBarMessageChanged(QString)));
+    if (!this->MainWindow.isNull())
+      {
+      disconnect(this->MainWindow->statusBar(), SIGNAL(messageChanged(QString)),
+                 this, SLOT(statusBarMessageChanged(QString)));
+      }
     }
 }
 

+ 1 - 1
Libs/Widgets/ctkErrorLogStatusMessageHandler.h

@@ -44,7 +44,7 @@ public slots:
   void statusBarMessageChanged(const QString& text);
 
 private:
-  QMainWindow* MainWindow;
+  QPointer<QMainWindow> MainWindow;
 };
 
 #endif