Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override QApplication::notify in Qt

I am trying to handle exception in my Qt application, I went through a couple of posts which indicated of overriding the QApplication::notify method to handle exceptions in a efficient way in Qt. I am not sure where should I add this overriden method. Is it the mainwindow.h or main.cpp? I added the following function in my MainWindow.h:

bool
notify(QObject * rec, QEvent * ev)
{
  try
  {
    return QApplication::notify(rec,ev);
  }
  catch(Tango::DevFailed & e)
  {
    QMessageBox::warning(0,
                         "error",
                         "error");
  }

  return false;
}

When I build my project I get the following error:

error: cannot call member function 'virtual bool QApplication::notify(QObject*, QEvent*)' without object

I am new to c++ and Qt.Could you let me know how I could implement this so that all my exceptions would be handled in an efficient way and the application does not crash.

like image 292
Valla Avatar asked Dec 22 '14 17:12

Valla


People also ask

What is QApplication in Qt?

The QApplication class manages the GUI application's control flow and main settings. QApplication contains the main event loop, where all events from the window system and other sources are processed and dispatched. It also handles the application's initialization, finalization, and provides session management.

What is exec () in Qt?

exec() is an infinite loop of the following style: do { get event from system handle event } while (true); If you use a queued connection, then the event is added to your event queue, and it will be performed at some point in the future during the app. exec() loop. There is no second thread in your program.

What is the purpose of the exec () method of the QApplication class?

[static] int QApplication::exec() Enters the main event loop and waits until exit() is called, then returns the value that was set to exit() (which is 0 if exit() is called via quit()). It is necessary to call this function to start event handling.


3 Answers

This is a method of a QApplication object. In order to override the notify method you must inherit from QApplication and in your main() you should instantiate a class as the Qt Application

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

int main(int argc, char* argv) {
    Application app(argc, argv);
    // Your initialization code
    return app.exec();
}
like image 170
McLeary Avatar answered Oct 17 '22 22:10

McLeary


error: cannot call member function 'virtual bool QApplication::notify(QObject*, QEvent*)' without object

That error message is trying to write that you are trying to call a non-static method without an actual object. Only static methods could work like that. Even if it was intended like that, which it is not, it could not be a static method anyway as C++ does not support virtual static methods (sadly, but that is another topic).

Therefore, I would personally do something like this:

main.cpp

#include <QApplication>
#include <QEvent>
#include <QDebug>

class MyApplication Q_DECL_FINAL : public QApplication
{
    Q_OBJECT
public:
    MyApplication(int &argc, char **argv) : QApplication(argc, argv) {}

    bool notify(QObject* receiver, QEvent* event) Q_DECL_OVERRIDE
    {
        try {
            return QApplication::notify(receiver, event);
        //} catch (Tango::DevFailed &e) {
            // Handle the desired exception type
        } catch (...) {
            // Handle the rest
        }        

         return false;
     }
};

#include "main.moc"

int main(int argc, char **argv)
{
    MyApplication application(argc, argv);
    qDebug() << "QApplication::notify example running...";
    return application.exec();
}

main.pro

TEMPLATE = app
TARGET = main
QT += widgets
SOURCES += main.cpp

Build and Run

qmake && make && ./main
like image 4
lpapp Avatar answered Oct 17 '22 22:10

lpapp


Just like with other event handlers in Qt, you need to define a child class derived from QApplication and implement bool notify (QObject *receiver, QEvent *e) there, then use your class instead of QApplication.

like image 1
Anton Poznyakovskiy Avatar answered Oct 17 '22 22:10

Anton Poznyakovskiy