PDA

View Full Version : Message handler question



bruccutler
18th May 2007, 16:48
I have created a message handler and I want to display the error in the output window in developer studio. What would you suggest I use to display the messages. Using qDebug causes and infinite loop back to the message handler. Doing a fprintf() gets lost since Microsoft doesn't see it.

Any ideas?
- BRC

marcel
18th May 2007, 16:52
qDebug should work.
How do you use it?
And what exactly do you mean by message handler?

jacek
18th May 2007, 17:05
Using qDebug causes and infinite loop back to the message handler.
That's how the message handler works --- it collects messages from qDebug(), qFatal() and similar and processes them in some way. On windows the default message handler should send those messages to the debugger.

bruccutler
18th May 2007, 17:26
Thanks.

Let me be more specific:
*** questions below ***

I call:
qInstallMsgHandler(myMsgHandler);
to install my own message handler, which follows:

void myMsgHandler(QtMsgType type , const char *msg)
{
QString text;
if (strstr(msg, "Object::connect:") != NULL)
{
return;
}
switch (type)
{
case QtDebugMsg:
text = QString ("Debug: %1\n").arg(msg);
break;
case QtWarningMsg:
text = QString ("Warning: %1\n").arg(msg);
break;
case QtCriticalMsg:
text = QString ("Critical: %1\n").arg(msg);
break;
case QtFatalMsg:
text = QString ("Fatal: %1\n").arg(msg);
break;
}

// Display a message to the user
if (this thread is gui thread) *** How do I get the thread to see if this is a GUI thread? ***
{
QMessageBox::information(NULL, QString("Error"), text); // this only works if this handler was called from a GUI thread
}
else
{
*** how to display error to user from a non-gui thread? ***
}

#ifndef NDEBUG // when in debug mode, do this
#ifdef WINDOWS
OutputDebugStringA(text.toAscii().data()); // this works if we are in debug
#else // non-windows
fprintf(stderr(text.toAscii()));
#endif
}
#endif
if (type == QtFatalMsg)
{
abort();
}
return;
}

jpn
18th May 2007, 18:27
// only windows has a "default" msg handler, 0 is returned on other platforms!
QtMsgHandler msgHandler = qInstallMsgHandler(myMsgHandler);

void myMsgHandler(QtMsgType type , const char *msg)
{
...

if (QThread::currentThread() == qApp->thread())
{
// gui thread
}

if (msgHandler)
{
// call the default windows handler
msgHandler(type, msg);
}
else
{
fprintf(stderr, qPrintable(text));
}

...
}

jacek
18th May 2007, 18:51
how to display error to user from a non-gui thread?
You can send an event with error message (for example to a QApplication subclass) and show that message box in the event handler.

marcel
18th May 2007, 19:44
You can send an event with error message (for example to a QApplication subclass) and show that message box in the event handler.


Or you can emit a signal from that thread if you don't want to implement a custom event.

bruccutler
19th May 2007, 21:18
Emitting a signal might be a good way to go.

Thanks.
- Bruce

bruccutler
19th May 2007, 21:20
One other quick question - how can I tell if the current thread is the GUI thread?

jacek
19th May 2007, 21:23
One other quick question - how can I tell if the current thread is the GUI thread?
See the post #5.

bruccutler
19th May 2007, 22:26
Oops -- Seeing I could not see. Now I've seen.