Results 1 to 11 of 11

Thread: Message handler question

  1. #1
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Message handler question

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Message handler question

    qDebug should work.
    How do you use it?
    And what exactly do you mean by message handler?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Message handler question

    Quote Originally Posted by bruccutler View Post
    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.

  4. #4
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Talking Re: Message handler question

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

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Message handler question

    Qt Code:
    1. // only windows has a "default" msg handler, 0 is returned on other platforms!
    2. QtMsgHandler msgHandler = qInstallMsgHandler(myMsgHandler);
    3.  
    4. void myMsgHandler(QtMsgType type , const char *msg)
    5. {
    6. ...
    7.  
    8. if (QThread::currentThread() == qApp->thread())
    9. {
    10. // gui thread
    11. }
    12.  
    13. if (msgHandler)
    14. {
    15. // call the default windows handler
    16. msgHandler(type, msg);
    17. }
    18. else
    19. {
    20. fprintf(stderr, qPrintable(text));
    21. }
    22.  
    23. ...
    24. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Message handler question

    Quote Originally Posted by bruccutler View Post
    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.

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Message handler question

    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.

  8. #8
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Message handler question

    Emitting a signal might be a good way to go.

    Thanks.
    - Bruce

  9. #9
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Message handler question

    One other quick question - how can I tell if the current thread is the GUI thread?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Message handler question

    Quote Originally Posted by bruccutler View Post
    One other quick question - how can I tell if the current thread is the GUI thread?
    See the post #5.

  11. The following user says thank you to jacek for this useful post:

    bruccutler (19th May 2007)

  12. #11
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Message handler question

    Oops -- Seeing I could not see. Now I've seen.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.