如何在Qt中覆盖QApplication :: notify [英] How to override QApplication::notify in Qt

查看:647
本文介绍了如何在Qt中覆盖QApplication :: notify的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Qt应用程序中处理异常,我经历了几篇文章,指出了重写QApplication :: notify方法以有效地在Qt中处理异常。我不确定应该在哪里添加此重写的方法。是mainwindow.h还是main.cpp?我在MainWindow.h中添加了以下函数:

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

我是c ++和Qt的新手,能否让我知道如何实现这一点,以便有效处理所有异常方式,应用程序不会崩溃。

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.

推荐答案

这是QApplication对象的方法。为了重写notify方法,您必须从 QApplication 继承,并且在您的 main()中,应将类实例化为Qt应用程序

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();
}

这篇关于如何在Qt中覆盖QApplication :: notify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆