Application crashes when exception is raised



  • I have developed a application using Qt 5.3. I am using try catch block to handle the exceptions. So whenever there is an exception I am trying to show a QMessageBox to the user with a relevant message. This message is shown to the user but after 2 or 3 seconds the application crashes(Mainwindow crashes). I went through a couple of posts which recommended to override QApplication::notify() method to handle exceptions. I have overriden the method and still the same issue.Did anyone face this issue before? What is that I am missing here.This is the code I am using:

    main.cpp
    @
    #include "mainwindow.h"
    #include <QApplication>
    #include <ctime>
    #include <iostream>
    #include <QPixmap>
    #include <stdio.h>
    #include <QProcess>
    #include <QString>
    #include <tango.h>
    #include <QPainter>
    #include <QPen>
    #include <QBrush>
    #include <QLabel>
    #include <QTimer>
    #include <ctime>
    #include <QMessageBox>

    class Application final : public QApplication {
    Application(int& argc, char** argv) : QApplication(argc, argv) {}
    virtual bool notify(QObject *receiver, QEvent *e) override {
    // your code here

        try
            {
                return QApplication::notify( receiver, e );
            }
            catch(Tango::DevFailed e)
            {
              //some message
            }
            catch(Tango::CommunicationFailed e)
            {
                //some message
            }
    
    }
    

    };

    int main(int argc, char *argv[]) try
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }

    catch (Tango::DevFailed e)
    {
    //QMessageBox::information(this,tr("Updating values failed"),tr("Error updating values.Try again."));
    }
    catch (Tango::CommunicationFailed e)
    {
    //QMessageBox::information(this,tr("Communication Failed"),tr("Error updating values.Try again."));
    }

    @

    updatescreen.cpp
    @
    void updatescreen::on_pushButton_clicked()
    {
    //update values

      if(!ui->lineEdit_15->text().isEmpty())
      {
         //update method called
    

    //if method fails then error is thrown by the method in API which is handled by the QApplicatoin::notify in the main.cpp

      }
    

    }
    @

    Could you let me know what would be the reason for application crash. I followed many posts but could not resolve it.Thanks.

    [Added @ around code. andreyc]



  • You should instantiate your own application class, not QApplication in the main function. Given that your class with the notify(..) method reimplemented is called Application the main should look like this:

    int main(int argc, char* argv[])
    {
        Application app(argc, argv);
        MainWindow w;
    
        w.show();
    
        return app.exec;
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.