Search code examples
c++qtqthreadqapplication

QThread and implementation of notify


What to look out for when reimplementing notify function in multi threaded Qt application? This is an example implementation. Currently there are no errors, but I am worried that an error could arise since multi threading in Qt uses signal slot for communication which uses notify function.

TApplication::notify(QObject *receiver, QEvent *event)
{
    bool returnValue(false);
    try
    {
        returnValue = QApplication::notify(receiver, event);
    }
    catch (IExceptionBase& e)
    {
        if (!fMain.isNull())
        {
            //report error to output and file log
        }
        else
        {
            //report error to output
        }
    }
    catch (...)
    {
        if (!fMain.isNull())
        {
            //report error to output and file log
        }
        else
        {
            //report error to output
        }
    }
    return returnValue;
}

fMain is a module with reporting functionality


Solution

  • In Qt5, this is safe. However, from the documentation, in Qt6 this will no longer work outside the main thread, and in fact, the function is being considered for deprecation in Qt6 entirely.

    As Kuba Ober pointed out, reimplementing notify to catch exceptions is a bad idea, as events in other threads and any queued signals are delivered asynchronously.