PDA

View Full Version : Help: how to pop a prompt dialog when the application crashes in Qt



many_many
27th July 2011, 03:22
Hello all,

I want to pop up a dialog to prompt the application crashed.
As the same time, I want to write the logo to locale file.
How should I do?
Thanks in advance.

NullPointer
27th July 2011, 06:27
Hi,

There is no way to catch all errors an app can cause... for example, an

try{
int a=10/0;
catch(...){}

will close the program without entering the catch block.

However, you may be interested in something like csignal.h (http://www.cplusplus.com/reference/clibrary/csignal/) that can catch many signals from system, then handle them, creating a new QWidget and doing a QApplication::exec()...

for example, in your main.cpp:


#include <all yout include files>
#include <signal.h>

void catchError(int b){
qDebug()<<"Got error #: "<<b;
throw "Some Error here";
}

int main(int argc, char *argv[])
{
signal(SIGSEGV, catchError);
try{ // here, the default behaviour
QApplication a(argc, argv);
MyMainWindow w;
w.show();
a.exec();
}catch(...){ // here, after a SegFault...
QApplication a(argc, argv);
QWidget b; //start here your Widget that will show the error...
b.show();
a.exec(); // Event loop again :)
}

return 0; // it is interesting to control with an int ret here...
}


The signal() will "connect" (not in QT way) that function with the error... The function will be called exactly where the error has occurred, so we need to throw something to enforce we will exit the first event loop.
After that, we can open a new widget and start a second event loop... :)

HTH.