简体   繁体   English

我应该在哪里重新实现QApplication :: notify功能?

[英]Where am I supposed to reimplement QApplication::notify function?

Where am I supposed to reimplement QApplication::notify function? 我应该在哪里重新实现QApplication :: notify功能? What I mean is, which class? 我的意思是,哪个班级? One of my own classes or subclass some of Qt's class and do it there? 我自己的一个类或者是Qt类的一部分并在那里完成它? I need this because I'm getting the following error while downloading file from a server(small files are downloaded ok, but large ones cause this msg): 我需要这个,因为从服务器下载文件时出现以下错误(小文件下载正常,但大文件导致此消息):

Qt has caught an exception thrown from an event handler. Qt捕获了一个从事件处理程序抛出的异常。 Throwing exceptions from an event handler is not supported in Qt. Qt不支持从事件处理程序中抛出异常。 You must reimplement QApplication::notify() and catch all exceptions there. 您必须重新实现QApplication :: notify()并捕获所有异常。

Just subclass QApplication and in your notify(..) method do something like this: 只需将QApplication子类化,并在您的notify(..)方法中执行以下操作:

try {
    return QApplication::notify( receiver, event );
} catch ( std::exception& e ) {
    showAngryDialog( e );
    return false;
}

Then use it in your main function instead of QApplication . 然后在主函数中使用它而不是QApplication

As said before create you own application object that inherits from QtApplication and redefine 'notify'. 如前所述,创建自己的应用程序对象,该对象继承自QtApplication并重新定义'notify'。 It is the way to go. 这是要走的路。 However be very sure to use this constructor: 但是请务必使用此构造函数:

MyApplication::MyApplication(int &argc, char *argv[]);

Setting argc as reference with '&' is important as it avoids a crash on some platforms. 将argc设置为带有'&'的引用非常重要,因为它可以避免某些平台上的崩溃。

The full procedure is described here: http://qt-project.org/forums/viewthread/17731 完整程序如下所述: http//qt-project.org/forums/viewthread/17731

My own implementation: 我自己的实施:

class MyApplication : public QApplication
{
public:
    MyApplication(int &argc, char ** argv);
    // ~MyApplication();
private:
    bool notify(QObject *receiver_, QEvent *event_);
};

(The crash described above happened on Ubuntu 13.10 64bits but was not present on version 12.04 64 bits.) (上面描述的崩溃发生在Ubuntu 13.10 64位上,但在版本12.04 64位上没有出现。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM