Results 1 to 3 of 3

Thread: Error notification in QT

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2012
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Error notification in QT

    Dear all,

    I would kite to know know can I notify errors from objects of classes derived from QObjects. Searching in the net, many times I've seen that if is suggested to overload the QCoreApplication::notify() member function (http://stackoverflow.com/questions/4...error-handling), but i don't like it much, I would prefer to notify errors by emiting signals. For example:

    Qt Code:
    1. class Foo : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. Q_SIGNALS:
    6. void error(QString string);
    7.  
    8. Q_SLOTS:
    9. void do_something()
    10. {
    11. try
    12. {
    13. a = ...;
    14. <do something>
    15. }
    16. catch (std::exception &e)
    17. {
    18. Q_EMIt error(QString::fromStdString(e.what()));
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    Does anyone uses this policy? How do you use it? One signal per exception type? How do you notify errors from Qt objects?

    Thanks and Best Regards,
    Joaquim Duran

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Error notification in QT

    It depends on whether the component you develop is meant to be used synchronously or asynchronously.
    - In the synchronous case (direct method calls that perform an operation and return a result), you can use exceptions or any other mechanism you would use in plain C++.
    - In the asynchronous case (i.e. call a slot to start an operation, and expect a signal later on to let you know that it has completed), then dedicated error signals are good practice (see QAbstractSocket::error(QAbstractSocket::SocketErro r) for example). You may use one signal with a parameter like QAbstractSocket, or several (one per error type); it is up to you. Just realize that the more signals you create, the more boilerplate code (in the form of connect() statements) you need. Clearly if all you need to know about the error is just a type, using an enum like QAbstractSocket::SocketError is the easiest option.

    The approach you outine in your code is quite elegant: use exceptions in your own code and convert them to signals so that they do not propagate through Qt's internals (which might cause terrible, terrible damage).

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Error notification in QT

    I don't like it as a general approach. You will get code bloat from lots more connect(...) statements, and also you have to connect them TO something. So now all(?) of your objects need to be able to receive error signal as well. Additionally your methods that 'throw' still have to satisfy any guarantee on return values. e.g. all code like this has to be refactored
    Qt Code:
    1. X getX(Key k)
    2. {
    3. Iter it = find(k);
    4. if (it == end)
    5. throw exception("oops");
    6.  
    7. return *it; // oh dear, we cant return if exception is caught and signal emitted instead
    8. // so we have to make everything return a bool...
    9. }
    To copy to clipboard, switch view to plain text mode 

    You only have to reimplement notify if your code throws whilst inside event handlers (iirc).


    How do you notify errors from Qt objects?
    Don't know what that means. In your suggestion you are using signals slots. So use signals/slots for error signalling and receiving.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Notification settings
    By mak_user in forum General Discussion
    Replies: 2
    Last Post: 9th April 2011, 17:30
  2. Notification in KDE
    By mpele in forum KDE Forum
    Replies: 0
    Last Post: 23rd November 2009, 10:24
  3. Notification for network disconnection
    By yogesh in forum Qt Programming
    Replies: 2
    Last Post: 12th March 2009, 06:55
  4. QT taskbar notification
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2007, 20:41
  5. Desktop Notification
    By jwintz in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2006, 06:13

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
  •  
Qt is a trademark of The Qt Company.